public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Reseller", "{id}", new { controller = "Reseller", action = "Index", id = UrlParameter.Optional }, new { id = @"^(\{{0,1}([0-9a-fA-F]){8}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){4}-([0-9a-fA-F]){12}\}{0,1})$" } ); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); }
or if you want a more robust constraint than any critical regular expression:
public class GuidConstraint : IRouteConstraint { public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { var value = values[parameterName] as string; Guid guid; if (!string.IsNullOrEmpty(value) && Guid.TryParse(value, out guid)) { return true; } return false; } }
and then:
routes.MapRoute( "Reseller", "{id}", new { controller = "Reseller", action = "Index", id = UrlParameter.Optional }, new { id = new GuidConstraint() } );
Darin Dimitrov
source share