Can I deploy a javascript file within MVC areas?

前端 未结 4 559
你的背包
你的背包 2021-01-13 09:13

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:

  • Differ
相关标签:
4条回答
  • 2021-01-13 09:32

    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.

    0 讨论(0)
  • 2021-01-13 09:35
    <script type="text/javascript" src='<%: ResolveUrl("~/Scripts/jquery/_Roles.js") %>'>
    </script>
    
    0 讨论(0)
  • 2021-01-13 09:36

    Have you try

    ResolveUrl(
    

    instead of

    Url.Content( 
    

    ?

    Stefano

    0 讨论(0)
  • 2021-01-13 09:48

    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.

    0 讨论(0)
提交回复
热议问题