MVC Route to Action for Javascript file

后端 未结 1 538
我在风中等你
我在风中等你 2020-12-21 16:53

I am trying to add a mvc route to generate a javascript from the controller. I have added the following route and it doesn\'t work:

routes.MapRouteWithName(
         


        
相关标签:
1条回答
  • 2020-12-21 17:41

    how do I make this work?

    IIS intercepts the request because it contains a file extension and hijacks it thinking it is a static file and not passing it to your application.

    To make it work you should tell IIS not to do that. Inside the <system.webServer> section you could add the following handler to indicate that requests with the specified pattern should be handled by the managed pipeline:

    <system.webServer>
        <handlers>
            ...
            <add name="ScriptsHandler" path="Scripts/Entities/*/datasource.js" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
        </handlers>
    </system.webServer>
    

    Some people might also tell you to use:

    <modules runAllManagedModulesForAllRequests="true" />
    

    but I wouldn't recommend you doing that because this means that all requests to static resources will now be flowing through the managed pipeline which could have a negative performance overhead for your application. The handler syntax allows you to selectively enable this only for certain route patterns and HTTP verbs.

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