Registering the main handler in Web.Config

I added a generic handler (ashx) in my project, but I don't see it registering in the web.config and it works. How so? I mean, shouldn't visual studio add it as an http handler in web.config? Or is it because I am not overriding any predefined handlers, instead I am calling it specifically.

+2


source to share


3 answers


Typically, a common handler in Asp.net is designed to support some small task, such as creating multiple thumbnails, that do not require an Asp.net process. So you can call it a call to a simple asp.net page like "www.somesite.com/Thumbnail.ashx?filename=abc.jpg".

By the way, if you want to map this handler to 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 kind of url routing to map it like web form routing (based on System.Routing). So you can use the following code to execute like in the above example.



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

      

For more information on Web Form Mapping, see Using Web Forms Routing by Phil Haack.

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

PS. If you are using a common handler to register in your web.config file, you need to create 2 files SomeHandler.ashx and SomeHandler.ashx.cs. It is quite difficult to create a simple file handler. On the other hand, you can only create one cs file that inherits from the IHttpHandler class to accomplish the same.

+6


source


A handler is another type of file that you can view. The HTTP module must be registered in the web.config file, but not necessarily in the handler.



+1


source


I believe you can think of an HTTP module instead of an HTTP handler

The HTTP module intercepts every HTTP request to the site and can additionally perform some operations in the HTTP pipeline or completely intercept the call. Modules must be registered in web.config

On the other hand, HTTPHandler works in much the same way as an ASPX page, except that it is a bit lighter and does not handle all the page events that you don't need for something like an image handler.

+1


source







All Articles