2

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.

crimbo
  • 10,308
  • 8
  • 51
  • 55
  • How do you know it will hurt the performance of those static files? Have you tried it both ways, profiled each, and determined that running all managed modules is too slow (which I honestly doubt)? If you haven't yet, I wouldn't worry about it. There are plenty of sites with high traffic that have that setting turned on. – rossipedia Apr 01 '14 at 05:03
  • 1
    possible duplicate of [MVC Route to Action for Javascript file](http://stackoverflow.com/questions/14745178/mvc-route-to-action-for-javascript-file) – Craig W. Apr 01 '14 at 05:13
  • I haven't tested that recently. That's a fair point, but also very dependent on what OWIN, MVC, Web API filters are running, and what route matching. Knowing that high traffic sites use it isn't that relevant - a high traffic site likely also uses separate servers and CDNs for static files. Can you provide more info on that statement? In case anyone is interested, here's an indication that runAllManagedModulesForAllRequests may not be ideal: http://www.hanselman.com/blog/BackToBasicsDynamicImageGenerationASPNETControllersRoutingIHttpHandlersAndRunAllManagedModulesForAllRequests.aspx – crimbo Apr 01 '14 at 05:14
  • Thanks Craig - I didn't see that one, and looked around a bit. The resolution is the same, so definitely seems similar. Diff is attribute routing and I was looking for file extensions. Any advice for figuring out whether this is indeed a dup or not? – crimbo Apr 01 '14 at 05:20

1 Answers1

2

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.

crimbo
  • 10,308
  • 8
  • 51
  • 55
  • What is the difference between TransferRequestHandler & UrlRoutingHandler? Both seem to work fine. – Serg Aug 03 '17 at 20:37