Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

Go to top

ASP.NET 4.0 HttpHandler Routing Support

, 7 Nov 2011
Rate this:
Please Sign up or sign in to vote.
Hi guys, I have a new version of this.Although it works as expected for the majority of cases, I found a problem on the previous version of this code, that is, it reuses the HttpHandler for different requests and that can be a problem on more complex implementations where inner properties are...
Hi guys, I have a new version of this.
Although it works as expected for the majority of cases, I found a problem on the previous version of this code, that is, it reuses the HttpHandler for different requests and that can be a problem on more complex implementations where inner properties are set and cached between requests.
 
So now every request gets its own fresh HttpHandler ensuring that everything is on its reset state.
 
namespace System.Web.Routing
{
	public class HttpHandlerRoute<T> : IRouteHandler where T: IHttpHandler
	{
		private String _virtualPath = null;
 
		public HttpHandlerRoute(String virtualPath)
		{
			_virtualPath = virtualPath;
		}
 
		public HttpHandlerRoute() { }
 
		public IHttpHandler GetHttpHandler(RequestContext requestContext)
		{
			return Activator.CreateInstance<T>();
		}
	}
 
	public class HttpHandlerRoute : IRouteHandler
	{
		private String _virtualPath = null;
 
		public HttpHandlerRoute(String virtualPath)
		{
			_virtualPath = virtualPath;
		}
 
		public IHttpHandler GetHttpHandler(RequestContext requestContext)
		{
			if (!string.IsNullOrEmpty(_virtualPath))
			{
				return (IHttpHandler)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(IHttpHandler));
			}
			else
			{
				throw new InvalidOperationException("HttpHandlerRoute threw an error because the virtual path to the HttpHandler is null or empty.");
			}
		}
	}
 
	public static class RoutingExtension
	{
		public static void MapHttpHandlerRoute(this RouteCollection routes, string routeName, string routeUrl, string physicalFile, RouteValueDictionary defaults = null, RouteValueDictionary constraints = null)
		{
			var route = new Route(routeUrl, defaults, constraints, new HttpHandlerRoute(physicalFile));
			routes.Add(routeName, route);
		}
 
		public static void MapHttpHandlerRoute<T>(this RouteCollection routes, string routeName, string routeUrl, RouteValueDictionary defaults = null, RouteValueDictionary constraints = null) where T : IHttpHandler
		{
			var route = new Route(routeUrl, defaults, constraints, new HttpHandlerRoute<T>());
			routes.Add(routeName, route);
		}
	}
}
 
Usage
// using the handler url
routes.MapHttpHandlerRoute("DoSomething", "Handlers/DoSomething", "~/DoSomething.ashx");
 
// using the type of the handler
routes.MapHttpHandlerRoute<MyHttpHanler>("DoSomething", "Handlers/DoSomething");

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

AlexCode
Architect
Switzerland Switzerland
Senior IT Consultant working in Switzerland as Senior Software Engineer.
 
Find more at on my blog.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web03 | 2.8.140926.1 | Last Updated 7 Nov 2011
Article Copyright 2011 by AlexCode
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid