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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • Why do you use underscores in your filenames? – ZippyV Nov 09 '11 at 08: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. – Samantha J T Star Nov 09 '11 at 09:20
  • 1
    @SamanthaJ what does the @Url.Content() render on the client side? Maybe your issue lies there. – shuniar Nov 09 '11 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. – Pawel Krakowiak Jan 19 '12 at 09:14

4 Answers4

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.

Community
  • 1
  • 1
Alexander Puchkov
  • 5,913
  • 4
  • 34
  • 48
  • I added these changes to the web.config that resides in the /Areas/AreaName/web.config and it works as well. – BrandonG Jan 16 '16 at 01:35
0

Have you try

ResolveUrl(

instead of

Url.Content( 

?

Stefano

Stefano.net
  • 1,078
  • 1
  • 11
  • 25
  • 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. – Samantha J T Star Nov 09 '11 at 08:59
0
<script type="text/javascript" src='<%: ResolveUrl("~/Scripts/jquery/_Roles.js") %>'>
</script>
Brandon
  • 68,708
  • 30
  • 194
  • 223
cemsazara
  • 1,623
  • 15
  • 14
-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.

mccow002
  • 6,754
  • 3
  • 26
  • 36
  • 2
    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... – Pawel Krakowiak Jan 19 '12 at 09:05
  • 15
    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. – Matt Randle Jun 18 '12 at 11:12
  • 3
    If you're architecture follows the SOA style of using autonomous business components this makes a ton of sense – George Mauer Aug 14 '12 at 21:16
  • It is convenient to have page specific JavaScript file next to view in the same folder – Alexander Puchkov Apr 27 '15 at 14:10
  • why has this been downvoted so much? Is there something wrong with keeping area specific scripts in the areas folder ? – SkeetJon Jan 25 '19 at 10:58