Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yeah, I noticed that, too. Perhaps there is a built-in ASP.NET way to do this, but the trick for me was to create a new class derived from IRouteHandler:</p> <pre><code>using System; using System.IO; using System.Reflection; using System.Text.RegularExpressions; using System.Web; using System.Web.Routing; namespace MyNamespace { class GenericHandlerRouteHandler : IRouteHandler { private string _virtualPath; private Type _handlerType; private static object s_lock = new object(); public GenericHandlerRouteHandler(string virtualPath) { _virtualPath = virtualPath; } #region IRouteHandler Members public System.Web.IHttpHandler GetHttpHandler(RequestContext requestContext) { ResolveHandler(); IHttpHandler handler = (IHttpHandler)Activator.CreateInstance(_handlerType); return handler; } #endregion private void ResolveHandler() { if (_handlerType != null) return; lock (s_lock) { // determine physical path of ashx string path = _virtualPath.Replace("~/", HttpRuntime.AppDomainAppPath); if (!File.Exists(path)) throw new FileNotFoundException("Generic handler " + _virtualPath + " could not be found."); // parse the class name out of the .ashx file // unescaped reg-ex: (?&lt;=Class=")[a-zA-Z\.]* string className; Regex regex = new Regex("(?&lt;=Class=\")[a-zA-Z\\.]*"); using (var sr = new StreamReader(path)) { string str = sr.ReadToEnd(); Match match = regex.Match(str); if (match == null) throw new InvalidDataException("Could not determine class name for generic handler " + _virtualPath); className = match.Value; } // get the class type from the name Assembly[] asms = AppDomain.CurrentDomain.GetAssemblies(); foreach (Assembly asm in asms) { _handlerType = asm.GetType(className); if (_handlerType != null) break; } if (_handlerType == null) throw new InvalidDataException("Could not find type " + className + " in any loaded assemblies."); } } } } </code></pre> <p>To create a route for an .ashx:</p> <pre><code>IRouteHandler routeHandler = new GenericHandlerRouteHandler("~/somehandler.ashx"); Route route = new Route("myroute", null, null, null, routeHandler); RouteTable.Routes.Add(route); </code></pre> <p>The code above may need to be enhanced to work with your route arguments, but it's starting point. Comments welcome.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload