Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic Handler Registration in Web.Config

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.

like image 884
Tarik Avatar asked Aug 05 '26 22:08

Tarik


1 Answers

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.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!