问题
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
});
}
However, this gives me an HTTP 404 - Not Found.
I'd prefer to not use runAllManagedModulesForAllRequests (eg
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
) since that will hurt the perf of other static files in the web app.
回答1:
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" />
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>
The nice thing about this is that the IIS static file handling is still in place for all the static files.
来源:https://stackoverflow.com/questions/22777172/asp-net-mvc-path-with-file-extension