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?
-
Intresting, id like to see if this is possible myself. Curious though, why not just add it the web.config? as that only effects the one site / app not all of IIS – Jammin Dec 11 '09 at 13:26
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.
https://web.archive.org/web/20140227135526/http://www.uberasp.net/getarticle.aspx?id=49
- 2,405
- 1
- 25
- 40
- 54,393
- 15
- 113
- 135
-
Having trouble getting this approach to work for my situation. The requests I’m trying to reassign a handler for do not correspond to physical files or to any of my configured routes. Would `PostMapRequestHandler` not be firing in my case since no handler is found to map the request to? It seems the last event that fires for these requests is `PostResolveRequestCache` and if I try to reset `context.Handler` in that or any earlier event handler, it just gets ignored. – Lobstrosity Aug 16 '14 at 00:29
-
I was able to get it working by calling `context.RemapHandler()` (instead of setting `context.Handler` directly) in the `BeginRequest` event handler. – Lobstrosity Aug 16 '14 at 22:15
You can by using an IRouteHandler class.
- Implement the IRouteHandler interface in a new class and return the hander as the result of its
GetHttpHandlermethod - 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
- 2,893
- 4
- 33
- 43