Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain a reference to the default ASP.NET page handler or web-services handler?

Consider a Web.config file containing the following httpHandlers declaration:

<httpHandlers>
  <add verb="*" path="*" type="MyWebApp.TotalHandlerFactory"/>
</httpHandlers>

In other words, this handler factory wants to “see” all incoming requests so that it gets a chance to handle them. However, it does not necessarily want to actually handle all of them, only those that fulfill a certain run-time condition:

public sealed class TotalHandlerFactory : IHttpHandlerFactory
{
    public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
    {
        if (some condition is true)
            return new MySpecialHttpHandler();

        return null;
    }

    public void ReleaseHandler(IHttpHandler handler) { }
}

However, doing it like this completely overrides the default ASP.NET handler, which means that ASP.NET pages and web services no longer work. I just get a blank page for every URL that doesn’t fulfill the “some condition” in the “if”. Therefore, it seems that returning null is the wrong thing to do.

So what do I need to return instead so that ASP.NET pages and web services are still handled normally?

like image 338
Timwi Avatar asked Oct 09 '22 19:10

Timwi


2 Answers

I would have thought the easiest way would be for your class to inherit from System.Web.UI.PageHandlerFactory and then in an else clause just call base.GetHandler().

public sealed class TotalHandlerFactory : System.Web.UI.PageHandlerFactory
{
    public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
    {
        if (some condition is true)
            return new MySpecialHttpHandler();
        else
            return base.GetHandler(context, requestType, url, pathTranslated)
    }
}
like image 133
Chris Avatar answered Oct 13 '22 05:10

Chris


I had the same problem, and seems that doing that is not possible using an HttpHandlerFactory.

But, I found a workaround that solved the problem: Using an HttpModule to filter which requests should go to my custom HttpHandler:

First, remove the any reference to your HttpHandler from the web.config.

Then, add a reference to the following HttpModule inside the <Modules> section:

public class MyHttpModule : IHttpModule
{
    public void Dispose() { }

    public void Init(HttpApplication application)
    {
        application.PostAuthenticateRequest += new EventHandler(application_PostAuthenticateRequest);
    }

    void application_PostAuthenticateRequest(object sender, EventArgs e)
    {
        var app = sender as HttpApplication;
        var requestUrl = context.Request.Url.AbsolutePath;

        if (requestUrl "meets criteria")
        {
            app.Context.RemapHandler(new MyHttpHandler());
        }
    }

}

Finally, assume at your HttpHandler that all the incoming request fulfill your criteria, and handle there all the requests.

like image 23
Gerardo Grignoli Avatar answered Oct 13 '22 04:10

Gerardo Grignoli