asp.net-mvc – 在MVC3中使用两个可选参数的路由不起作用
发布时间:2020-05-24 17:57:57 所属栏目:asp.Net 来源:互联网
导读:我的应用程序中使用了以下类型的URL. localhost/admin/userdetail/id localhost/admin/userdetail/id/true localhost/admin/userdetail/id/true/success 这是我的管理员控制器 bool inSaveAction, string status are optional [Authorize]
|
我的应用程序中使用了以下类型的URL.
这是我的管理员控制器
[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" });
}
@Html.ActionLink("DisplayName","UserDetail",new { id = Model.Id })
在Global.asax中 routes.MapRoute("UserDetail","UserDetail/{id}",new
{
controller = "Admin",action = "UserDetail",id = UrlParameter.Optional
}
);
我跟着http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx 我如何制作inSaveAction& status作为UserDetail操作的可选参数? 解决方法您错过了路线配置中的参数.为了使这个工作具有可选的不同参数(如在Phil Haack的帖子中),您需要定义多个路径routes.MapRoute("UserDetail-WithStatus","UserDetail/{id}/{inSaveAction}/{status}",new
{
controller = "Admin",// nothing optional
}
);
routes.MapRoute("UserDetail-WithoutStatus","UserDetail/{id}/{inSaveAction}",// nothing optional
}
);
routes.MapRoute("UserDetail-WithoutSaveAction",id = UrlParameter.Optional
}
);
然后创建链接: @Html.ActionLink("Link","Index","Admin",new { id = 1,success = "success" },null)
您还需要将可选参数设置为可空,否则如果缺少id或inSaveAction,您将获得异常. public ActionResult UserDetail(int? id,bool? inSaveAction,string status)
{
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 有人有一种方法来保持页面呈现一旦一个人已退出
- .net – 应用程序池限制
- asp.net – Orchard CMS如何进行日志记录?
- asp.net阻止表单提交两次
- asp.net – 可接受的安全性:使用Paramatised SQL和HTML编码
- asp.net-web-api – 使用apicontroller对odata EntitySetCo
- ASP.net MVC v2 – 调试模型绑定问题 – BUG?
- asp.net – .NET“代码块块”?
- asp.net-mvc – asp.net mvc中HttpUnauthorizedResult上的默
- asp.net – 如何在IIS上配置Web部署发布功能,以便开发人员可
推荐文章
站长推荐
- asp.net-mvc – 如何在Controller外访问RequestC
- asp.net-web-api – 标题中的API密钥与swashbuck
- asp.net-mvc-2 – 如何在ASP.NET MVC2中枚举form
- asp.net-mvc – Html.CheckBox返回false如果禁用
- asp.net-mvc-4 – .NET MVC4 ActionNameSelector
- asp.net – 如何在Kendo UI中获取下拉菜单的选定
- asp.net – 转义HTML实体并避免WebForm标签中的H
- 依赖于文件的.net核心asp.net单元测试 – appset
- asp.net-mvc – 如何在我自己的自定义助手中使用
- asp.net – 无法为自定义MembershipProvider创建
热点阅读
