MVC Route to Action for Javascript File

I am trying to add mvc route to create javascript from controller. I added the following route and it does not work:

routes.MapRouteWithName( "DataSourceJS", // Route name "Scripts/Entities/{controller}/datasource.js", // URL with parameters new { controller = "Home", action = "DataSourceJS"} // Parameter defaults, , null ); 

But if I change the route so as not to have ".js" and I go to "Scripts / Entities / {controller} / datasource", it works. But I need to have a .js extension, how do I do this?

+2
source share
1 answer

How can i do this?

IIS intercepts the request because it contains the file extension and captures it, thinking it is a static file and does not transfer it to your application.

To make it work, you must tell IIS not to do it. Inside the <system.webServer> section, you can add the following handler to indicate that requests with the specified template should be processed by a 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 may also tell you:

 <modules runAllManagedModulesForAllRequests="true" /> 

but I would not recommend that you do this, because it means that all requests for static resources will now go through a managed pipeline, which can have negative operating costs for your application. The handler syntax allows you to selectively enable this only for specific route patterns and HTTP verbs.

+5
source

Source: https://habr.com/ru/post/975690/


All Articles