In ASP.NET MVC, how can I load script from my view

2019-06-08 16:21发布

I've refactored some of my View's javascript out into .js files alongside the views (not in the Scripts folder off root).

By default, the handler in web.config for the views stops these being loaded:

<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>

However, I want to override this to allow the browser to request .js files from this location.

Does anyone know how I can do this?

Thanks, Mark.

2条回答
甜甜的少女心
2楼-- · 2019-06-08 16:57

I think I would leave the scripts in the scripts location but if you wanted to move them you could do the following:

At the top of the web.config file under views look for

<httpHandlers>
  <add path="*" verb="*"
      type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

And replace it with the following:

<httpHandlers>
  <add path="*.aspx" verb="*"
      type="System.Web.HttpNotFoundHandler"/>
    <add path="*.master" verb="*"
        type="System.Web.HttpNotFoundHandler"/>
    <add path="*.ascx" verb="*"
        type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

You will need to add the extension of each file type you want to keep blocked.

查看更多
Rolldiameter
3楼-- · 2019-06-08 17:16

You could change the path attribute there to be something less comprehensive than "*" (everything), but why are you fighting the conventions of the framework? The Views folder is intended to organize (only) the view files, and is specifically not meant to be accessed directly from the outside. Is there some reason why you can't put those script files elsewhere in your app?

查看更多
登录 后发表回答