在MVC3中使用两个可选参数的路由不起作用

时间:2022-03-22 08:28:35

I have a following types of url used in my Application.

我的应用程序中使用了以下类型的URL。

localhost/admin/userdetail/id

localhost/admin/userdetail/id/true

localhost/admin/userdetail/id/true/success

Here is my Admin Controller

这是我的管理控制器

bool inSaveAction, string status are optional

bool inSaveAction,字符串状态是可选的

    [Authorize]
    public ActionResult UserDetail(string Id, bool inSaveAction, string status)
    {
    }

    [HttpPost, Authorize, ValidateAntiForgeryToken]
    public ActionResult SaveUserDetail(UserDetailViewModel viewModel)
    {
        User userToSave = new User();
        AdminService.UpdateUser(userToSave);
        //This is calling the above function as it sending all 3 params
        return RedirectToAction("UserDetail", new { Id = viewModel.Id, 
                           inSaveAction = true, status = "success" });
    }

Below case is not working

以下情况不起作用

  @Html.ActionLink("DisplayName", "UserDetail", new { id = Model.Id })

In Global.asax

 routes.MapRoute("UserDetail",
            "UserDetail/{id}",
            new
            {
                controller = "Admin",
                action = "UserDetail",
                id = UrlParameter.Optional
            }
         );

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

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

How can i make inSaveAction & status as optional parameter for my UserDetail action?

如何将inSaveAction&status作为UserDetail操作的可选参数?

2 个解决方案

#1


9  

You're missing the parameters in your route config. In order to make this work with different parameters optional (as in Phil Haack's post), you need to define multiple routes

您缺少路线配置中的参数。为了使这个工作具有可选的不同参数(如在Phil Haack的帖子中),您需要定义多个路径

routes.MapRoute("UserDetail-WithStatus", 
                "UserDetail/{id}/{inSaveAction}/{status}", 
                 new
                 {
                     controller = "Admin",
                     action = "UserDetail",
                     // nothing optional 
                 }
);

routes.MapRoute("UserDetail-WithoutStatus", 
                "UserDetail/{id}/{inSaveAction}", 
                 new
                 {
                     controller = "Admin",
                     action = "UserDetail",
                     // nothing optional 
                 }
);

routes.MapRoute("UserDetail-WithoutSaveAction", 
                "UserDetail/{id}", 
                 new
                 {
                     controller = "Admin",
                     action = "UserDetail",
                     id = UrlParameter.Optional
                 }
);

And then create links with:

然后创建链接:

@Html.ActionLink("Link", "Index", "Admin", new { id = 1, inSaveAction = true, success = "success" }, null)

You'll also need to set the optional parameters as nullable, otherwise you'll get exceptions if id or inSaveAction are missing.

您还需要将可选参数设置为可空,否则如果缺少id或inSaveAction,您将获得异常。

public ActionResult UserDetail(int? id, bool? inSaveAction, string status)
{

}

#2


1  

You can use the approach introduced here. It allows you to define one route like this

您可以使用此处介绍的方法。它允许您定义这样的一条路线

routes.MapRoute(name: "UserDetail-WithStatus", 
            url: "UserDetail/{id}/{inSaveAction}/{status}", 
             defaults: new
             {
                 controller = "Admin",
                 action = "UserDetail",
                 // nothing optional 
             }, 
             lookupParameters:new string[] { "id", "inSaveAction", "status" },
             routeValueService: new RouteValueService()

);

#1


9  

You're missing the parameters in your route config. In order to make this work with different parameters optional (as in Phil Haack's post), you need to define multiple routes

您缺少路线配置中的参数。为了使这个工作具有可选的不同参数(如在Phil Haack的帖子中),您需要定义多个路径

routes.MapRoute("UserDetail-WithStatus", 
                "UserDetail/{id}/{inSaveAction}/{status}", 
                 new
                 {
                     controller = "Admin",
                     action = "UserDetail",
                     // nothing optional 
                 }
);

routes.MapRoute("UserDetail-WithoutStatus", 
                "UserDetail/{id}/{inSaveAction}", 
                 new
                 {
                     controller = "Admin",
                     action = "UserDetail",
                     // nothing optional 
                 }
);

routes.MapRoute("UserDetail-WithoutSaveAction", 
                "UserDetail/{id}", 
                 new
                 {
                     controller = "Admin",
                     action = "UserDetail",
                     id = UrlParameter.Optional
                 }
);

And then create links with:

然后创建链接:

@Html.ActionLink("Link", "Index", "Admin", new { id = 1, inSaveAction = true, success = "success" }, null)

You'll also need to set the optional parameters as nullable, otherwise you'll get exceptions if id or inSaveAction are missing.

您还需要将可选参数设置为可空,否则如果缺少id或inSaveAction,您将获得异常。

public ActionResult UserDetail(int? id, bool? inSaveAction, string status)
{

}

#2


1  

You can use the approach introduced here. It allows you to define one route like this

您可以使用此处介绍的方法。它允许您定义这样的一条路线

routes.MapRoute(name: "UserDetail-WithStatus", 
            url: "UserDetail/{id}/{inSaveAction}/{status}", 
             defaults: new
             {
                 controller = "Admin",
                 action = "UserDetail",
                 // nothing optional 
             }, 
             lookupParameters:new string[] { "id", "inSaveAction", "status" },
             routeValueService: new RouteValueService()

);