文件扩展ASP.NET MVC路径 [英] ASP.NET MVC path with file extension
问题描述
在ASP.NET MVC5使用基于属性的路由,我要处理的文件扩展名的URL,例如:
In ASP.NET MVC5 using attribute-based routing, I want to handle URLs with file extensions, eg
~/javascript/security.js
下面是一个例子控制器的操作方法:
Here's an example controller action method:
[Route("javascript/security.js")]
public ActionResult AngularSecurityModule(string clientId)
{
return View(new
{
ClientId = clientId
});
}
然而,这给了我一个HTTP 404 - 未找到
However, this gives me an HTTP 404 - Not Found.
我倒是preFER不使用runAllManagedModulesForAllRequests(例如:
I'd prefer to not use runAllManagedModulesForAllRequests (eg
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
),因为这会伤害其他静态文件的PERF的Web应用程序。
) since that will hurt the perf of other static files in the web app.
推荐答案
原来的答案是,我只需要注册该网址的正确处理,即加入
Turns out the answer is that I just need to register the right handler for that URL, ie adding
<add name="JavascriptSecurityJs" path="javascript/security.js" verb="GET" type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
我的 system.webServer /处理器
的伎俩。为了完整起见,这里是整个 system.webServer
块中的的web.config
:
to my system.webServer/handlers
did the trick. For completeness, here's the whole system.webServer
block in the web.config
:
<system.webServer>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
<add name="JavascriptSecurityJs" path="javascript/security.js" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
关于这样做的好处是,IIS静态文件的处理还是很到位的所有静态文件。
The nice thing about this is that the IIS static file handling is still in place for all the static files.
这篇关于文件扩展ASP.NET MVC路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!