ASP.NET MVC path with file extension

白昼怎懂夜的黑 提交于 2019-12-11 03:57:54

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!