简体   繁体   中英

How many routing parameters can you have?

How many routing parameters can you have in .NET MVC? Like localhost:port/controller/action/parameter1/parameter2/parameter3/ and so on

I'm currently using this for my routing

routes.MapRoute(
     name: "Default",
     url: "{controller}/{action}/{id}/{extra}/{extra2}",
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, extra = UrlParameter.Optional, extra2 = UrlParameter.Optional }
);

And this works great. But when I add yet another routing like an "extra3" in this example the whole page just breaks, ordinary form posts and so on stops working.

Is there a limit to how many you can have and should I rather just make normal querystrings?

Edited

To explain it further. I want to do this

routes.MapRoute(
     name: "Default",
     url: "{controller}/{action}/{id}/{extra}/{extra2}/{extra3}",
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, extra = UrlParameter.Optional, extra2 = UrlParameter.Optional, extra3 = UrlParameter.Optional }
);

But it just simply does not work when I add even further amount of routes/parameters. Is there a limit to how many you can have?

HTML Helpers eg ( Html.ActionLink, Html.Action, Html.BeginForm etc ) gives wrong results when there are more optional parameters in routs. Normally we keep one optional parameter at the end of the route as optional.

If you keep the routing config as follows it will work.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
         name: "Default",
         url: "{controller}/{action}/{id}/{extra}/{extra2}/{extra3}",
         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, extra = UrlParameter.Optional, extra2 = UrlParameter.Optional, extra3 = UrlParameter.Optional }
    );

    routes.MapRoute(
         name: "OneOptional",
         url: "{controller}/{action}/{id}",
         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

}

Read the section " The Root Cause " of following post understand the issue with multiple optional parameters.

http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx/

Thanks!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM