Update To RegExRoute Class
Update with a few new constructors that allow you to use the MvcRouteHandler, and some bug fixes.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.Routing; using System.Web.Mvc; using System.Text.RegularExpressions; namespace WebUtility { public class RegexRoute : RouteBase { public Regex Regex { get; private set; } public String[] Groups { get; private set; } private IRouteHandler RouteHandler { get; set; } public String Controller { get; private set; } public String Action { get; private set; } /// <summary> /// Creates A Regular Expression Route /// </summary> /// <param name="regex">Regular Expression To Use Against /// the AbsolutePath of the request.</param> /// <param name="groups">roups In The Match</param> /// <param name="routeHandler">The name of the Handler /// to use for this route.</param> public RegexRoute(Regex regex, String[] groups, IRouteHandler routeHandler) { Regex = regex; Groups = groups; RouteHandler = routeHandler; } /// <summary> /// Constructor that allows you to specify the /// controller and the action. /// </summary> /// <param name="regex">Regular Expression</param> /// <param name="groups">Groups In The Match</param> /// <param name="controller">Explicit Name Of The Controller</param> /// <param name="action">Explict Name Of The Action</param> public RegexRoute(Regex regex, String[] groups, String controller, String action) { Regex = regex; Groups = groups; Controller = controller; Action = action; } /// <summary> /// Constructor that assume controller and /// action are in the groups. /// </summary> /// <param name="regex"></param> /// <param name="groups"></param> public RegexRoute(Regex regex, String[] groups) { Regex = regex; Groups = groups; List<String> list = new List<String>(groups); if (!list.Contains("controller")) throw (new Exception( "Controller group expected in regular expression match")); if (!list.Contains("action")) throw (new Exception( "Action group expected in regular expression match")); } public override RouteData GetRouteData( System.Web.HttpContextBase httpContext) { MatchCollection matchCollection = Regex.Matches(httpContext.Request.Url.AbsolutePath); switch (matchCollection.Count) { case 0: // WWB: There Is No Match -- // This Route Doesn't Handle This URI return (null); case 1: // WWB: FillOut The Route Data RouteData routeData = new RouteData(); routeData.Route = this; if (RouteHandler != null) routeData.RouteHandler = RouteHandler; else routeData.RouteHandler = new MvcRouteHandler(); if (!String.IsNullOrEmpty(Controller)) routeData.Values.Add("controller", Controller); if (!String.IsNullOrEmpty(Action)) routeData.Values.Add("action", Action); // WWB: No Group Names, No Values Outputted. if (Groups != null) { // MSDN:The GroupCollection object returned // by the Match.Groups property // always has at least one member. if (matchCollection[0].Groups.Count != Groups.Length) throw (new Exception(String.Format( "{0} contains {1} groups when matching {2}, however " + "there are only {3} mappings. There needs to be an " + "equal number of mappings to groups, note that " + "there is always one group the whole string.", httpContext.Request.Url.AbsoluteUri, matchCollection[0].Groups.Count, Regex.ToString(), Groups.Length))); // WWB: Map All The groups into the values for the RouteData for (Int32 index = 0; index < matchCollection[0].Groups.Count; index++) { routeData.Values.Add(Groups[index].ToString(), matchCollection[0].Groups[index]); } } return (routeData); default: throw (new Exception( String.Format("There Multiple Matches For {0} on {1}," + "which means that the regular expression has more " + "then one non-overlapping match.", Regex.ToString(), httpContext.Request.Url.AbsoluteUri))); } } public override VirtualPathData GetVirtualPath( RequestContext requestContext, RouteValueDictionary values) { throw new NotImplementedException(); } } }{6230289B-5BEE-409e-932A-2F01FA407A92}
Comments
Post a Comment