简体   繁体   English

MVC路由可以处理的参数数量是否有限制?

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

I am writing an MVC Application using MVC Areas. 我正在使用MVC区域编写MVC应用程序。 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: 但是,我们发现需要具有6个参数的控制器动作:

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. 我们发现可以将其添加到当前设置中,但是第四个id之后的url中的任何参数都只是串联到第四个id参数中。

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? 关于MVC路由可以映射到的参数数量,它是否具有某种形式的CAP? 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. 我们还发现,如果将映射参数的数量缩小到id,id2和id3,它将再次起作用,所有溢出都串联到id3中。

Does anyone have any information around this? 有人对此有任何信息吗?

ASP.NET MCV has no limit on the number of parameters. ASP.NET MCV对参数数量没有限制。

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. 变量id是可选的,但id4是必需的。 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 }
 );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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