Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC Route Error: The constraint entry 'Length'

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:

  1. Am I doing anything out of the ordinary or is my methodology here in line with standard practice?

  2. What is the problem here?

Thanks,

Sachin

like image 270
Sachin Kainth Avatar asked Jan 18 '12 11:01

Sachin Kainth


People also ask

What is route constraint in MVC?

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.

Can we add constraints to the route in MVC?

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.

What is route constraint and what is the use of it?

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.

What is route value in MVC?

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.


1 Answers

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" }
        );
    }
like image 114
Sergey Kudriavtsev Avatar answered Sep 20 '22 15:09

Sergey Kudriavtsev