Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I deploy a javascript file within MVC areas?

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?

like image 519
Samantha J T Star Avatar asked Nov 26 '22 16:11

Samantha J T Star


1 Answers

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.

like image 140
Alexander Puchkov Avatar answered Jan 10 '23 14:01

Alexander Puchkov