简体   繁体   中英

Is there a limit on the number of parameters an MVC route can handle?

I am writing an MVC Application using MVC Areas. Currently we are using the following route for our display area:

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "Display_default",
        "Display/{controller}/{action}/{id}/{id2}/{id3}/{*id4}",
        new { action = "Index", id = UrlParameter.Optional , id2 = UrlParameter.Optional, id3 = UrlParameter.Optional }
    );
}

And this works. However we discovered the need for a controller action with 6 parameters:

public ActionResult _MyMethod(string id, string id2, string id3, string id4, string id5, string id6)

we discovered we can add this we the current setup, however any parameter in the url after the forth id is just concatenated into the 4th id parameter.

We tried to extend the route to accept the number of parameters that we needed like so:

public override void RegisterArea(AreaRegistrationContext context) 
{
    context.MapRoute(
        "Display_default",
        "Display/{controller}/{action}/{id}/{id2}/{id3}/{id4}/{id5}/{*id6}",
        new { action = "Index", id = UrlParameter.Optional , id2 = UrlParameter.Optional, id3 = UrlParameter.Optional }
    );
}

However, using this route the page no longer loads.

Does MVC Routing have some sort of CAP as to the amount of parameters it can map to? We also discovered that if we shrink the number of mapped parameters to id, id2, and id3 it will work however again with all overflow concatenated into id3.

Does anyone have any information around this?

ASP.NET MCV has no limit on the number of parameters.

In your case

public override void RegisterArea(AreaRegistrationContext context) 
{
   context.MapRoute(
      "Display_default",
      "Display/{controller}/{action}/{id}/{id2}/{id3}/{id4}/{id5}/{*id6}",
       new { action = "Index", id = UrlParameter.Optional , id2 = UrlParameter.Optional, id3 = UrlParameter.Optional }
   );
}

Variable id is optional however id4 is mandatory. You have to add optional parameters into the end.

Like

 routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id5}/{id6}/{id}/{id2}/{id3}/{*id4}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, id2 = UrlParameter.Optional, id3 = UrlParameter.Optional, id4 = UrlParameter.Optional, id5 = UrlParameter.Optional , id6 = UrlParameter.Optional }
);

Or make all of parameters optional

routes.MapRoute(
      name: "Default",
      url: "{controller}/{action}/{id}/{id2}/{id3}/{id4}/{id5}/{*id6}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, id2 = UrlParameter.Optional, id3 = UrlParameter.Optional, id4 = UrlParameter.Optional, id5 = UrlParameter.Optional }
 );

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