Am i right in thinking regex routes have been superseeded by route constraints?
there is nothing on the internet about routing via regex? there seems to be some posts pre v1 but nothing since and i have achieved the same goal using constraints.
RegEx Routes were obsoleted and the preferred way to handle this is through route constraints.
One of the challenges of RegEx Routes was the ability to use the Url Helpers / Action Links to generate the correct url for your page.
I'm sure that the information you've found is this discussion about MVC Beta.
public class RegexRoute : Route
{
private readonly Regex _urlRegex;
public RegexRoute(string urlPattern, IRouteHandler routeHandler)
: this(urlPattern, null, routeHandler)
{}
public RegexRoute(string urlPattern, RouteValueDictionary defaults, IRouteHandler routeHandler)
: base(null, defaults, routeHandler)
{
_urlRegex = new Regex(urlPattern, RegexOptions.Compiled);
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
string requestUrl = httpContext.Request.AppRelativeCurrentExecutionFilePath.Substring(2) + httpContext.Request.PathInfo;
Match match = _urlRegex.Match(requestUrl);
RouteData data = null;
if(match.Success)
{
data = new RouteData(this, this.RouteHandler);
// add defaults first
if (null != this.Defaults)
{
foreach (KeyValuePair<string, object> def in this.Defaults)
{
data.Values[def.Key] = def.Value;
}
}
// iterate matching groups
for (int i = 1; i < match.Groups.Count; i++)
{
Group group = match.Groups[i];
if (group.Success)
{
string key = _urlRegex.GroupNameFromNumber(i);
if (!String.IsNullOrEmpty(key) && !Char.IsNumber(key, 0)) // only consider named groups
{
data.Values[key] = group.Value;
}
}
}
}
return data;
}
}
Incidentally, a great "side effect" of having the route defined as a regular expression is that it allows us to perform validation in-place, which kind of renders the concept of Constraints unnecessary.
Here's a quick example route defined as a regular expression:
routes.Add(new RegexRoute(@"^Books/((?<ssn>[\d]{3}(-?)[\d]{2}\1[\d]{4})|(?<query>.+)?)$", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(new { controller = "Book", action = "Find" })
});
Now the route above could, and possibly should, have been split into two different routes; one for finding books by SSN and a different one for searching book titles, but I wanted to demonstrate the flexibility gained by using regular expressions. With the above route, urls like "mysite.com/Books/Last+Argument+Of+Kings" and "mysite.com/Books/0575077905" both map to the Find action on the BookController class, with the appropriate parameter initialized:
public class BookController : Controller
{
public void Find(string query, int? ssn)
{
// ... gets the book by ssn if present, otherwise searches using the query
}
}
from: http://www.iridescence.no/post/Defining-Routes-using-Regular-Expressions-in-ASPNET-MVC.aspx retrieved from http://web.archive.org/web/20110227054359/http://www.iridescence.no/post/Defining-Routes-using-Regular-Expressions-in-ASPNET-MVC.aspx