Good to Know: Built-in ASP.NET Http Handlers

Http handlers is a really elegant infrastructure allowing you to provide special behavior to specific paths in your application. You can use them to create RSS  feeds, dynamically generate images, handle requests differently and  more.

Apart from writing new http handlers, there are some built-in handlers that you can take advantage of. The built in handlers are separated into three categories (in my opinion at least): request handlers, error generators and misc. handlers.

Request Handlers

These handlers process requests in a specific way. Although these already have paths configured  for them, you can manually configure them in the web.config (or via  IIS Manager) to run on different  paths. For example, treat ABC files the same as ASPX files. There is one catch here – because ASPX, ASHX and ASMX files are dynamically compiles, you will also need to provide build configurations for the new extensions.

The handlers in this category include:

  • The regular ASP.NET page (ASPX) hander - System.Web.UI.PageHandlerFactory.
  • The generic handler (ASHX) handler - System.Web.UI.SimpleHandlerFactory.
  • The resource handler (WebResource.axd) - System.Web.Handlers.AssemblyResourceLoader.
  • The web service handler (ASMX) - System.Web.Services.Protocols.WebServiceHandlerFactory.
  • The trace handler (trace.axd) – System.Web.Handlers.TraceHandler.

The following web.config  sample enables accessing trace info via MyTrace.aaa (this is web.config configuration for IIS 7 running in Integrated  mode, in other versions or modes the configuration will be a bit different):

    <system.webServer>
      <handlers>
        <add verb="*" path="MyTrace.aaa" name="Trace-handler" type="System.Web.Handlers.TraceHandler"/>
      </handlers>
    </system.webServer>

After this is configured (and tracing is enabled as well), try navigate to MyTrace.aaa page.

    Error Generators

    These handlers generate specific http errors. You can use them to prevent access to specific files, specific extensions or folders. These error generator handlers include:

  • Forbidden – generates a 403 Forbidden http error - System.Web.HttpForbiddenHandler.
  • Not Found – generates a 404 Not Found  http error - System.Web.HttpNotFoundHandler.
  • Method Not Allowed – generates a 405 Method Not Allowed http error - System.Web.HttpMethodNotAllowedHandler.
  • Not Implemented – generates a 501 Not Implemented http error - System.Web.HttpNotImplementedHandler.

The following web.config sample prevents users from accessing all files with secret extensions. Once a user tries to access such file he or she will get a 403 Forbidden page:

<system.webServer>
  <handers>
    <add verb="*" path="*.secret" name="SecretAccess" type="System.Web.HttpForbiddenHandler"/>
  </handlers>
</system.webServer>

Try navigating to top.secret and witness the result.

    Miscellaneous

    The misc. category contains one handler, the static file handler, which can help in various  scenarios. It will present the file content without any processing. With this handler you can, for example, enable users to retrieve lkr file (no special meaning to this extension that I know of) content.

  • Static file – shows the content of a file without any processing - System.Web.StaticFileHandler.

The following  sample configures lkr files to be processed by the static file handler. To test this, create a txt file in your web application root folder, rename its extension to lkr and navigate to it.

<system.webServer>
  <handlers>
    <add verb="*" path="*.lkr" name="lkr-handler" 
              type="System.Web.StaticFileHandler" resourceType="File"/>
  </handlers>
</system.webServer>

 

All  the best,
Shay.

Share it:
Shout it kick it on DotNetKicks.com




Comments

November 30. 2010 12:00 PM

pingback

Pingback from earleshulte.wordpress.com

UFC 123 Press Conference Video | earleshulte

earleshulte.wordpress.com

November 30. 2010 03:07 PM

pingback

Pingback from marinidaagucin.wordpress.com

CrunchGear Week in Review: Egg Timer Edition | marinidaagucin

marinidaagucin.wordpress.com

November 30. 2010 03:49 PM

pingback

Pingback from veggastudio.com

Internet Explorer 9 preview 7 available, takes SunSpider crown; Beta now downloaded 13 million times | Energizer Blog

veggastudio.com

November 30. 2010 04:00 PM

pingback

Pingback from emerysugahara.wordpress.com

Teen Mom Amber Portwood’s Daughter Returns Home | emerysugahara

emerysugahara.wordpress.com

November 30. 2010 06:00 PM

pingback

Pingback from alverabedoya.wordpress.com

Voogle brings Google Voice to webOS | alverabedoya

alverabedoya.wordpress.com

December 3. 2010 10:05 AM

pingback

Pingback from donnettebarma.wordpress.com

Beyonce Inspired By Michael Jackson, Lauryn Hill For New Album | Donnettebarma's Blog

donnettebarma.wordpress.com

November 2. 2011 03:28 AM

pingback

Pingback from wb21.80666.net

Kim Kardashian to file for divorce after 2 months
    (AP) | Wb 21

wb21.80666.net

November 16. 2011 02:57 PM

pingback

Pingback from watchfreehorrormoviescomputer.elindependiente.info

Obama to vow bigger Asia footprint on Pacific tour
    (Reuters) | Watch Free Horror Movies Computer

watchfreehorrormoviescomputer.elindependiente.info

February 4. 2013 02:53 PM

mortgage loans new york

Now i hve to agree that this built in asp.net is really powerful and is going to be very helpful.

mortgage loans new york

March 16. 2013 09:42 AM

Ms. Coco

It is a great post to me. I am astonish for this post.I want that will give us more important information.So carry on

Ms. Coco

April 2. 2013 01:45 PM

cell phone website design

This is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! Keep up the good work.

cell phone website design

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



Subscribe Subscribe

That's Me!

Hi! I'm Shay Friedman
I'm Shay Friedman - a Visual C#/IronRuby MVP, a consultant and instructor of .NET technologies, author, speaker and new technologies freak
More about me

Contact Me

> Contact page
> Twitter: @ironshay
> LinkedIn profile

Search

Hosted By

I'm hosting this site on Arvixe and I'm very happy with it.
If you're looking for ASP.NET hosting, I highly recommend it
(and if you order from this link I also get some beer money!)
Web Hosting