Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
942 views
in Technique[技术] by (71.8m points)

asp.net mvc - MVC Route to Action for Javascript file

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(
           "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 to not have the ".js" and I navigate to "Scripts/Entities/{controller}/datasource" it works. But I need to have the .js file extension on there, how do I make this work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...