ryan miller
ryan miller

Reputation: 105

404 with JavaScript file in MVC

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

This is how I reference it in my view

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

Everything I have seen online says this should work but it is not. Am I missing a reference somewhere else or what is the deal?

Thanks

Upvotes: 3

Views: 3545

Answers (1)

heymega
heymega

Reputation: 9391

The problem is you're storing js files in your view folder. The view folder has it's own web.config which stops you from requesting static files from it.

Your options

  1. Modify the web.config to allow the js files through
  2. Move your js files out of the view folder.
  3. Use MVC bundling which should be able to access the view folder and have it bundle the script into a different repo.

Number 2 is best practice in my opinion. Was there a particular reason why you have the script in the view folder to begin with?

Update

To allow js files in you view folder add the following to the web.config located in your area's views 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>

Upvotes: 5

Related Questions