asp.net custom HttpHandler and URL routing

11,535

So you have a class that implements IHttpHandler called: MyHandler and it's in the namespace Example, you need to make the following entries in the site's Web.Config in the httpHandlers section:

<httpHandlers>
  <add verb="*" path="*" type="Example.MyHandler"/>
</httpHandlers>

Since this redirects all URLs for your web site/application to your handler you have to consider how to serve static content (imgs, scripts, style sheets etc). One way is to store such static content in a consistent URL like http://example.com/static/..., you can then set your handlers as such:

<httpHandlers>
  <add verb="*" path="*" type="Example.MyHandler"/>
  <add verb="GET,HEAD" path="static/*" type="System.Web.StaticFileHandler" />
</httpHandlers>

For your local dev webserver (embedded in Visual Studio) this is all that's needed. For IIS, you also need to tell IIS how to deal with these URLS (since the server first analyses a request to decide where to send it - including whether to send it to ASP.NET or some other extension).

  • Open: IIS Manager ->
  • Section: Websites ->
  • Right click on your website ->
  • Option: Properties ->
  • Tab: Home Directoy ->
  • Button: [Configuration...] ->
  • Tab: Mappings ->
  • Section: "Wildcard application maps (order of implementation):" ->
  • Button: [Insert...] ->
  • Executable: "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" (or whichever version of the .NET runtime your handler uses) ->
  • Uncheck "Verify that file exists" ->
  • Button: [OK]

Now both IIS and ASP.NET know how to deal with your URLS.

The above approach means when requesting static files, ASP.NET is actually serving the files, not IIS - which leads to a few disadvantages (discussed here). You can override this behaviour (disable wildcard mapping from the static directory) by switching the directory to an application (in IIS Manager), removing the wildcard mapping statement (added above) and switching it back from an application. Voilà - static files are handled by IIS without pestering your ASP.NET.

Share:
11,535
Admin
Author by

Admin

Updated on June 27, 2022

Comments

  • Admin
    Admin almost 2 years

    I want handle the requests to my application "http://example.com/whateverpath" by a custom HttpHandler but the return things depending of the value of "whateverpath".

    So users accessing "http://example.com/path1" will get different response than users accessing "http://example.com/path2", but both request must be handled in the same HttpHandler. The idea is find "whateverpath" in a database and depending of the result, return the response content.

    I hear about URL routing and I already have a custom Http handler working, but can I combine both technique to get what I need?

    I will appreciate any comment respect this issue.

    Cheers Frank Abel

  • Admin
    Admin over 13 years
    Can you explain "I would not use an HTTP handler for it."? As far I know HTTP handler provide better performance than a normal ASPX page and I don't see any disadvantage of the HTTP handler way.
  • Jonathan Wood
    Jonathan Wood over 13 years
    Page routing is more straight forward and provides more flexibility. It was designed for exactly what you described. It's possible for an HTTP handler to be faster, but that's because part of the support for ASP.NET pages is not run/loaded. I just got through implementing a custom HTTP handler. It works great. But I don't think it's the right approach for what you described.
  • Admin
    Admin over 13 years
    Thanks for your reply... can you elaborate more what you mean with "straight forward and provides more flexibility"? I see HTTP handler approach very easy and straight forward. Beside, can you be more specific of how would look like the URL routing solution? Rudu was really detailed in his answer. Thanks again Jonathan.
  • Jonathan Wood
    Jonathan Wood over 13 years
  • roufamatic
    roufamatic over 12 years
    Might be true but doesn't help with legacy situations.