3

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

2
  • just remove areas and then try Commented Jun 23, 2015 at 15:54
  • please provide a screen shot of your solution explorer with expanded nodes. Commented Jun 23, 2015 at 16:14

1 Answer 1

5

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>
Sign up to request clarification or add additional context in comments.

3 Comments

I just like having the file in the views folder because it is Area specific and won't be used in any other areas. How would I go about modifying the web.config to allow that? I have already added this <add name="JavaScriptHandler" path ="*.js" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" /> to the web.Config as I found that on other sites but it didn't work for me.
@ryanmiller I've updated my answer but it looks like you've added the correct configuration. Make sure you're adding it under system.webserver and that you're putting it in the correct web.config. It goes in the view's webconfig file
Ah HA! I figured out why it wasn't working for me. I had it as the HttpNotFoundHandler instead of StaticFileHandler. So thank you very much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.