views:

973

answers:

3

Hi,

I've added a generic handler (ashx) in my project but I can't see it's registration in web.config and it works. How come ? I mean shouldn't visual studio add it as a http handler in web.config ? Or it is because I doesn't override any pre-defined handlers instead I am calling it specifically .

Thanks in advance ?

+1  A: 

A handler is just another type of file you can browse to. An HTTP Module has to be registered in a web.config, but not necessarily a handler.

Jonas
But if I want to overwrite asp.net default httphandlers for whole project then I'll need to register it in web.config right ?
Braveyard
I'm not 100% sure what you're asking...are you adding a handler that has the same name as a built-in handler?
Jonas
Or are you adding a handler that's intended to map to a wildcard path? IE, any request to *.foo should be directed to your handler?
Jonas
I mean there must be default httphandler which handles all requests before passing to httpmodules, right ? and it handles all the requests unless otherwise isn't specified, what if I want to write my own custom handler which handles everthing in my project ? Then should I register it as default httphandler ?
Braveyard
That's where you're confusing an HTTP module with an HTTP handler. An HTTP handler is just a slimmed down page, really...a URL that you browse directly to. An HTTP module is code that runs during the request pipeline, and is run for every (managed) request. See here for an example: http://msdn.microsoft.com/en-us/library/aa719858(VS.71).aspx
Jonas
Thanks a lot for helping me.
Braveyard
+4  A: 

Generally, the generic handler in Asp.net is designed for supporting some small task like creating some thumbnail pictures that doesn't require Asp.net process. So, you can call it like call a simple asp.net page like "www.somesite.com/Thumbnail.ashx?filename=abc.jpg".

By the way, if you want to map this handler with some URL like the following URL.

  • www.somesite.com/Thumbnail/abc.jpg
  • www.somesite.com/Thumbnail/dog.jpg
  • www.somesite.com/Thumbnail/cat.jpg

You need to use some URL routing like web form routing (based on System.Routing) for mapping it. So, you can use the following code for doing like the above example.

public static void RegisterRoutes(RouteCollection routes)
{
  routes.Map("Thumbnail", "Thumbnail/{filename}").To("~/Thumbnail.ashx");
}

For more information about Web Form mapping, please look at Using Routing With Web Forms by Phil Haack.

However, if you need to create some Http handler that can handler some specify file type for your application like JavaScript file handler. You must create class that inherits from IHttpHandler. After that, you must register it in web.config file for specify file type that is handled by this handler. Please look at HTTP Handlers and HTTP Modules in ASP.NET By Mansoor Ahmed Siddiqui

PS. If you use generic handler for registering in web.config file, you need to create 2 files that are SomeHandler.ashx and SomeHandler.ashx.cs. It's quite complicate for creating some simple file handler. In the other hand, you can create only one cs file that inherits from IHttpHandler class for doing the same thing.

Soul_Master
Thanks, now that makes sense.
Braveyard
Good explaination 1+.....
Muhammad Akhtar
+1  A: 

I believe you may be thinking of an HTTP module instead of an HTTP Handler

An HTTP Module intercepts every HTTP request to the site and can optionally do some operations in the HTTP pipeline or intercept the call entirely. Modules must be registered in the web.config

On the other hand, an HTTPHandler works almost exactly the same as an ASPX page, except it is a bit more lightweight and doesn't process all the page events you do not need for something such as an image handler.

Chris Ballance
Thanks for the nice answer but actually I wasn't confusing Httphandler with httpModule I kinda know what they were. I was asking why I register httphandler :) well thanks tho.
Braveyard