404 with JavaScript file in MVC

I am having problems getting my page to use my javascript file. I have a file in Areas / Export / Views / Export folder and it is called Export.js

This is how I refer to it in my view

<script src="@Url.Content("~/Areas/Export/Views/Export/Export.js")"></script>

      

Everything I've seen on the internet says this should work, but it doesn't. Am I missing a link somewhere else or what is the deal?

thank

+3


source to share


1 answer


The problem is you are storing js files in your view folder. The view folder has its own web.config which prevents you from querying static files from it.

Your parameters

  • Modify web.config file to allow js files via
  • Move js files from view folder.
  • Use an MVC kit that needs to have access to the view folder and link it to a script in another repo.

Number 2 is best practice in my opinion. Did you have some reason why you have a script in your view folder?



Update

To allow js files in your view folder, add the following to the web.config file located in the area view folder.

<system.webServer>
<handlers>
  <add name="JavaScriptHandler" path="*.js" verb="*"
     preCondition="integratedMode" type="System.Web.StaticFileHandler" />      
  <remove name="BlockViewHandler"/>
  <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>

      

+3


source







All Articles