Click here to Skip to main content
15,884,472 members
Articles / Web Development / ASP.NET
Alternative
Tip/Trick

ASP.NET 4.0 HttpHandler Routing Support

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 Nov 2011CPOL 7.8K   1  
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.

C#
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
C#
// 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)


Written By
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 --