MVC中的多个可选参数不起作用

时间:2021-02-06 10:59:33

My requirement is to provide optional parameters to urls. urls should be like the.

我的要求是为网址提供可选参数。网址应该像。

  1. http://test.com/118939
  2. http://test.com/118939/test/2000/

I have written following routes

我写过以下路线

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

            routes.MapRoute(
                "FAQDefault",
                "FAQ",
                new { controller = "FAQ", action = "Default" });
            routes.MapRoute(null, "{id}", new { controller = "Home", action = "Default", id = UrlParameter.Optional });
            routes.MapRoute("rent", "{id}/{rent}/{unit}", new { controller = "Home", action = "Default", id = UrlParameter.Optional, rent = UrlParameter.Optional, unit = UrlParameter.Optional });
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Default", id = UrlParameter.Optional }, // Parameter defaults
                new string[] { "CDCPortal" });


        }

and controller written like:

和控制器写成:

 public ActionResult Default(string id, string rent=null,string unit=null){}

Its working fine for 1 url but not working for second url.

它的工作正常1个网址但不适用于第二个网址。

2 个解决方案

#1


0  

There is no need to do as, you have done in the first case:

在第一种情况下你没有必要这样做:

The second route can handle any number of parameters.

第二条路径可以处理任意数量的参数。

#2


0  

You need to define a route for each combinations like below

您需要为下面的每个组合定义路线

routes.MapRoute("Default-AllOptional", 
                "Default/{id}/{rent}/{unit}", 
                 new
                 {
                     controller = "Home",
                     action = "Default"
                     // nothing optional 
                 }
);

routes.MapRoute("Defaul-Id-rent-Optional", 
                "Default/{id}/{rent}", 
                 new
                 {
                     controller = "Home",
                     action = "Default",
                     id=UrlParameter.Optional,
                     rent=UrlParameter.Optional

                 }
);

Refer Routing Regression With Two Consecutive Optional Url Parameters

请参阅具有两个连续可选URL参数的路由回归

#1


0  

There is no need to do as, you have done in the first case:

在第一种情况下你没有必要这样做:

The second route can handle any number of parameters.

第二条路径可以处理任意数量的参数。

#2


0  

You need to define a route for each combinations like below

您需要为下面的每个组合定义路线

routes.MapRoute("Default-AllOptional", 
                "Default/{id}/{rent}/{unit}", 
                 new
                 {
                     controller = "Home",
                     action = "Default"
                     // nothing optional 
                 }
);

routes.MapRoute("Defaul-Id-rent-Optional", 
                "Default/{id}/{rent}", 
                 new
                 {
                     controller = "Home",
                     action = "Default",
                     id=UrlParameter.Optional,
                     rent=UrlParameter.Optional

                 }
);

Refer Routing Regression With Two Consecutive Optional Url Parameters

请参阅具有两个连续可选URL参数的路由回归