Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any way to add HttpHandler programmatically in .NET?

I've been researching this a bit but haven't come across an answer -- is there any way to programatically add an HttpHandler to an ASP.NET website without adding to the web.config?

like image 414
Ryan Lanciaux Avatar asked Dec 11 '09 13:12

Ryan Lanciaux


People also ask

What is HTTPHandler in ASP.NET MVC?

HTTPHandler is a low level request and response API in ASP.Net for injecting pre-processing logic to the pipeline based on file extensions and verbs. An HTTPhandler may be defined as an end point that is executed in response to a request and is used to handle specific requests based on extensions.

Which of the following elements registers the custom handler?

The configuration element registers the custom handler factory by class name and maps the . sample file name extension to that handler. Because you are registering a custom file name extension, you register the handler in both the handlers section and the httpHandlers section.


2 Answers

By adding an HttpHandler I assume you mean the configuration files

<system.web>
    <httpHandlers>...</httpHandler>
</system.web>

There is a way to control it automatically, by adding the IHttpHandler in directly during the request. So on the PostMapRequestHandler in the Application Lifecycle, you would do the following, in your own custom IHttpModule:

private void context_PostMapRequestHandler(object sender, EventArgs e)
{
    HttpContext context = ((HttpApplication)sender).Context;
    IHttpHandler myHandler = new MyHandler();
    context.Handler = myHandler;
}

And that would automatically set the handler for that request. Obviously you probably want to wrap this in some logic to check for things such as verb, requesting url, etc. But this is how it would be done. Also this is how many popular URL Rewriters work such as:

http://urlrewriter.codeplex.com

Unfortunately though, using the pre built configuration handler that the web.config does, is hidden away and doesn't seem to be accessible. It is based off an interface called IHttpHandlerFactory.

Update The IHttpHandlerFactory can be used just like any other IHttpHandler, only it is used as a launching point instead of a processing point. See this article.

http://www.uberasp.net/getarticle.aspx?id=49

like image 130
Nick Berardi Avatar answered Oct 12 '22 20:10

Nick Berardi


You can by using an IRouteHandler class.

  1. Implement the IRouteHandler interface in a new class and return the hander as the result of its GetHttpHandler method
  2. Register your route/

Implement IRouteHandler

public class myHandler : IHttpHandler, IRouteHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        // your processing here
    }

    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return this;
    }
}

Register Route:

//from global.asax.cs
protected void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.Add(new Route
    (
        "myHander.axd",
        new myHandler()
    ));
}

Note: if using Asp.Net Webforms then ensure your webapp has the UrlRouting configuration in the web.config as explained here: Use Routing with Web Forms

like image 33
Keith K Avatar answered Oct 12 '22 20:10

Keith K