5

I have my js files inside areas and I cannot access them. When I move them outside the MVC areas then I can access.

I have tried the following:

  • Different naming of js files - doesn't solve problem
  • Check to see if they exist on the server - they do
  • Access file directly from within IIS manager on server - they won't open and return not found
  • Access same files directly from within IIS manager on server but when files are in script directory - They open in browser
  • Used the route checker - When I try to access the file it does not open route debug and instead just says "404"

This works:

<script src="@Url.Content("~/Scripts/jquery/_Roles.js")" type="text/javascript"></script>

This does not work:

<script src="@Url.Content("~/Areas/Administration/Scripts/Roles/_Roles.js")" type="text/javascript"></script>

Could there be something different about files under the Areas folder that blocks scripts?

4
  • Why do you use underscores in your filenames? Commented Nov 9, 2011 at 8:59
  • Convention for me. However I will probably change that. Either way I noticed I still have the same problem. When deployed my application does not like scripts in Areas. however running locally on dev server it is okay. Commented Nov 9, 2011 at 9:20
  • 1
    @SamanthaJ what does the @Url.Content() render on the client side? Maybe your issue lies there. Commented Nov 9, 2011 at 20:40
  • The second one should work, it does for me in MVC3. I also have admin-related scripts inside the admin area (so that the regular users can't peek at them). Most likely you got the path wrong and should be looking at what was generated (vide shuniar's comment above) and whether the said file is accessible if you manually type the URL in the browser. Forgive me a comment after a few months, but I'm leaving it in case someone else tries to do this in the future. Commented Jan 19, 2012 at 9:14

4 Answers 4

1

Found an answer in another Stack Overflow question and tweaked it for areas.

Modify /Areas/AreaName/Views/web.config file to enable the webserver to serve JS and CSS files:

<system.web>
    <httpHandlers>
        <add path="*.js" verb="GET,HEAD" type="System.Web.StaticFileHandler" />
        <add path="*.css" verb="GET,HEAD" type="System.Web.StaticFileHandler" />
        <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>
    <!-- other content here -->
</system.web>

<system.webServer>
    <handlers>
        <remove name="BlockViewHandler"/>
        <add name="JavaScript" path="*.js" verb="GET,HEAD" type="System.Web.StaticFileHandler" />
        <add name="CSS" path="*.css" verb="GET,HEAD" type="System.Web.StaticFileHandler" />
        <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
    <!-- other content here -->
</system.webServer>

This will allow serving of .js and .css files, and will forbid serving of anything else.

Sign up to request clarification or add additional context in comments.

1 Comment

I added these changes to the web.config that resides in the /Areas/AreaName/web.config and it works as well.
0

Have you try

ResolveUrl(

instead of

Url.Content( 

?

Stefano

1 Comment

I will try that now. The code is running on Azure so it will take 15 minutes to upload. Checking the docs I'm not sure it will help as it says: If the relativeUrl parameter contains an absolute URL, the URL is returned unchanged. If the relativeUrl parameter contains a relative URL, that URL is changed to a relative URL that is correct for the current request path, so that the browser can resolve the URL.
0
<script type="text/javascript" src='<%: ResolveUrl("~/Scripts/jquery/_Roles.js") %>'>
</script>

Comments

-3

Why put your scripts in the Areas section? I have an mvc site with an area as well, but I still keep my scripts in the Scripts folder.

My suggestion is to rethink the reason you're organizing your content that way and consider moving all external .js files to the Scripts folder.

5 Comments

I can think of at least one reason - preventing users w/o administrative privileges from reading the admin panel scripts which might give them some ideas on what is going on there (paths, functionality, etc.). I actually came here looking for info on storing scripts within a MVC area...
An Area is a logical grouping of functionality so implementation issues aside, why wouldnt you want to put Area specific script into an Area folder? Seems common sense to me.
If you're architecture follows the SOA style of using autonomous business components this makes a ton of sense
It is convenient to have page specific JavaScript file next to view in the same folder
why has this been downvoted so much? Is there something wrong with keeping area specific scripts in the areas folder ?

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.