Mur*_*san 7 asp.net-mvc url-routing asp.net-mvc-routing asp.net-mvc-3
我的应用程序中使用了以下类型的URL.
本地主机/管理/ userdetail/ID
本地主机/管理/ userdetail/ID /真
本地主机/管理/ userdetail/ID /真/成功
这是我的管理员控制器
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" });
}
Run Code Online (Sandbox Code Playgroud)
以下情况不起作用
@Html.ActionLink("DisplayName", "UserDetail", new { id = Model.Id })
Run Code Online (Sandbox Code Playgroud)
在Global.asax中
routes.MapRoute("UserDetail",
"UserDetail/{id}",
new
{
controller = "Admin",
action = "UserDetail",
id = UrlParameter.Optional
}
);
Run Code Online (Sandbox Code Playgroud)
如何将inSaveAction&status作为UserDetail操作的可选参数?
您错过了路线配置中的参数.为了使这个工作具有可选的不同参数(如在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
}
);
Run Code Online (Sandbox Code Playgroud)
然后创建链接:
@Html.ActionLink("Link", "Index", "Admin", new { id = 1, inSaveAction = true, success = "success" }, null)
Run Code Online (Sandbox Code Playgroud)
您还需要将可选参数设置为可空,否则如果缺少id或inSaveAction,您将获得异常.
public ActionResult UserDetail(int? id, bool? inSaveAction, string status)
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15418 次 |
| 最近记录: |