I have a controller called Account with an action with the following signature:
public ActionResult Verify(string userName, Guid authorisationToken);
I have created a link to call this action thus:
/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.
This is what my RegisterRoutes method looks like in Global.asax:
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 anything out of the ordinary or is my methodology here in line with standard practice?
What is the problem here?
Thanks,
Sachin
The Route Constraint in ASP.NET MVC Routing allows us to apply a regular expression to a URL segment to restrict whether the route will match the request. In simple words, we can say that the Route constraint is a way to put some validation around the defined route.
Attribute Routing is introduced in MVC 5.0. We can also define parameter constraints by placing a constraint name after the parameter name separated by a colon. There are many builtin routing constraints available. We can also create custom routing constraints.
You use route constraints to restrict the browser requests that match a particular route. You can use a regular expression to specify a route constraint. For example, imagine that you have defined the route in Listing 1 in your Global.
RouteData is a property of the base Controller class, so RouteData can be accessed in any controller. RouteData contains route information of a current request.
You should change
"UI.Controllers"
to
new[] { "UI.Controllers" }
in your second route.
If you're specifying only a single string (not an array) then you get wrong overload of MapRoute
function - instead of MapRoute(RouteCollection, String, String, Object, String[])
which accept a list of namespaces as last parameter you get MapRoute(RouteCollection, String, String, Object, Object)
which expects constraints as last parameter. String "UI.Controllers" is not a correct constraint specification => you get the error.
Also as @Pankaj suggested your custom route should go 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" }
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With