问题描述
我想添加一个MVC的途径来从控制器的JavaScript。我加入了以下路线,这是行不通的:
routes.MapRouteWithName(
DataSourceJS,//路线名称
脚本/实体/ {}控制器/datasource.js,//带参数的URL
新{控制器=家,行动=DataSourceJS} //参数默认,
, 空值
);
但是,如果我改变路线没有的.js,我定位到脚本/实体/ {}控制/数据源它的工作原理。但是,我需要有.js文件扩展名在那里,我该如何使这项工作?
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.
这篇关于MVC路线行动为Javascript文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!