RegexRouter
public class RegexRoute : Route
{
#region Private Fields
private readonly Regex _urlRegex;
#endregion
#region Constructors
public RegexRoute(Regex urlPattern, IRouteHandler routeHandler)
: this(urlPattern, null, routeHandler)
{
}
public RegexRoute(Regex urlPattern, RouteValueDictionary defaults, IRouteHandler routeHandler)
: base(null, defaults, routeHandler)
{
_urlRegex = urlPattern;
}
#endregion
#region Public Overrides Methods
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var requestUrl = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo;
var match = _urlRegex.Match(requestUrl);
RouteData data = null;
if (match.Success)
{
data = new RouteData(this, RouteHandler);
// add defaults first
if (null != Defaults)
{
foreach (var def in Defaults)
{
data.Values[def.Key] = def.Value;
}
}
if (Constraints != null)
{
Dictionary<string, string> constraintDict = new Dictionary<string,string>();
(Constraints as ICollection<KeyValuePair<string, object>>).ToList().ForEach(
c => {
constraintDict.Add(c.Key,c.Value.ToString());
});
// iterate matching groups
if (constraintDict != null && constraintDict.Count > 0)
{
for (var i = 1; i < match.Groups.Count; i++)
{
var group = match.Groups[i];
if (!group.Success) continue;
foreach (var _cregex in constraintDict)
{
if (Regex.IsMatch(group.Value, _cregex.Value))
{
data.Values[_cregex.Key] = group.Value;
constraintDict.Remove(_cregex.Key);
if (constraintDict.Count == 0)
break;
else
continue;
}
}
}
}
}
}
return data;
}
#endregion
}
public static class RegexRouteCollectionExtensions
{
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern)
{
return routes.MapRoute(name, urlPattern, null, null);
}
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern, object defaults)
{
return routes.MapRoute(name, urlPattern, defaults, null);
}
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern, string[] namespaces)
{
return routes.MapRoute(name, urlPattern, null, null, namespaces);
}
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern, object defaults, object constraints)
{
return routes.MapRoute(name, urlPattern, defaults, constraints, null);
}
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern, object defaults, string[] namespaces)
{
return routes.MapRoute(name, urlPattern, defaults, null, namespaces);
}
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern, object defaults, object constraints, string[] namespaces)
{
if (routes == null)
{
throw new ArgumentNullException("routes");
}
if (urlPattern == null)
{
throw new ArgumentNullException("urlPattern");
}
var route2 = new RegexRoute(urlPattern, new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(defaults),
Constraints = new RouteValueDictionary(constraints)
};
var item = route2;
if ((namespaces != null) && (namespaces.Length > 0))
{
item.DataTokens = new RouteValueDictionary();
item.DataTokens["Namespaces"] = namespaces;
}
routes.Add(name, item);
return item;
}
}
{
#region Private Fields
private readonly Regex _urlRegex;
#endregion
#region Constructors
public RegexRoute(Regex urlPattern, IRouteHandler routeHandler)
: this(urlPattern, null, routeHandler)
{
}
public RegexRoute(Regex urlPattern, RouteValueDictionary defaults, IRouteHandler routeHandler)
: base(null, defaults, routeHandler)
{
_urlRegex = urlPattern;
}
#endregion
#region Public Overrides Methods
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var requestUrl = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo;
var match = _urlRegex.Match(requestUrl);
RouteData data = null;
if (match.Success)
{
data = new RouteData(this, RouteHandler);
// add defaults first
if (null != Defaults)
{
foreach (var def in Defaults)
{
data.Values[def.Key] = def.Value;
}
}
if (Constraints != null)
{
Dictionary<string, string> constraintDict = new Dictionary<string,string>();
(Constraints as ICollection<KeyValuePair<string, object>>).ToList().ForEach(
c => {
constraintDict.Add(c.Key,c.Value.ToString());
});
// iterate matching groups
if (constraintDict != null && constraintDict.Count > 0)
{
for (var i = 1; i < match.Groups.Count; i++)
{
var group = match.Groups[i];
if (!group.Success) continue;
foreach (var _cregex in constraintDict)
{
if (Regex.IsMatch(group.Value, _cregex.Value))
{
data.Values[_cregex.Key] = group.Value;
constraintDict.Remove(_cregex.Key);
if (constraintDict.Count == 0)
break;
else
continue;
}
}
}
}
}
}
return data;
}
#endregion
}
public static class RegexRouteCollectionExtensions
{
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern)
{
return routes.MapRoute(name, urlPattern, null, null);
}
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern, object defaults)
{
return routes.MapRoute(name, urlPattern, defaults, null);
}
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern, string[] namespaces)
{
return routes.MapRoute(name, urlPattern, null, null, namespaces);
}
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern, object defaults, object constraints)
{
return routes.MapRoute(name, urlPattern, defaults, constraints, null);
}
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern, object defaults, string[] namespaces)
{
return routes.MapRoute(name, urlPattern, defaults, null, namespaces);
}
public static Route MapRoute(this RouteCollection routes, string name, Regex urlPattern, object defaults, object constraints, string[] namespaces)
{
if (routes == null)
{
throw new ArgumentNullException("routes");
}
if (urlPattern == null)
{
throw new ArgumentNullException("urlPattern");
}
var route2 = new RegexRoute(urlPattern, new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(defaults),
Constraints = new RouteValueDictionary(constraints)
};
var item = route2;
if ((namespaces != null) && (namespaces.Length > 0))
{
item.DataTokens = new RouteValueDictionary();
item.DataTokens["Namespaces"] = namespaces;
}
routes.Add(name, item);
return item;
}
}