MVC Route Error: Length Restriction Record

I have a controller called Account with an action with the following signature:

public ActionResult Verify(string userName, Guid authorisationToken); 

I created a link to call this action:

 /Account/Verify/sachin13/409bdaaa-0b65-4bb8-8695-6e430323d8f8 

When I go to this link, I get the following error:

 The constraint entry 'Length' on the route with URL 'Account/{Verify}/{userName}/{authorisationToken}' must have a string value or be of a type which implements IRouteConstraint. 

Here's what the RegisterRoutes method in Global.asax looks like:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } ,// Parameter defaults new[] { "UI.Controllers" } ); routes.MapRoute( "AccountVerify", "Account/{Verify}/{userName}/{authorisationToken}", new { controller = "Account", action = "Verify", userName = "", authorisationToken = "" }, "UI.Controllers" ); } 

Two questions:

  • Am I doing something unusual or is my methodology here in accordance with standard practice?

  • What is the problem?

Thanks,

Sechin

+7
source share
2 answers

You have to change

"UI.Controllers"

to

new[] { "UI.Controllers" }

in the second route.

If you specify only one line (and not an array), you get the MapRoute function overloaded MapRoute - instead of MapRoute(RouteCollection, String, String, Object, String[]) , which takes a list of namespaces as the last parameter, you get MapRoute(RouteCollection, String, String, Object, Object) , which expects restrictions as the last parameter. The string "UI.Controllers" is not a valid specification of the constraint => you get an error.

Also, as @Pankaj suggested that your custom route follow before default, and Verify should be without "{}".

Full code:

 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "AccountVerify", "Account/Verify/{userName}/{authorisationToken}", new { controller = "Account", action = "Verify", userName = "", authorisationToken = "" }, new [] { "UI.Controllers" } ); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } ,// Parameter defaults new[] { "UI.Controllers" } ); } 
+12
source

Always indicate your route to the default route so that it works, working in order from first to last. So you need to declare your second route to the default route, and it should solve the problem, I suppose. Also, delete "{}" to confirm in the second route

+2
source

All Articles