asp.net-mvc – 在MVC3中使用两个可选参数的路由不起作用
发布时间:2020-12-15 21:03:55 所属栏目: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] public ActionResul
我的应用程序中使用了以下类型的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-mvc – ASP.NET MVC – 单元测试过度杀毒? (TDD)
- asp.net-mvc – 自定义DataAnnotations Validator派生自Reg
- 在ASP.NET MVC中为JavaScript生成动作URL
- asp.net成员资格 – 在Application_AuthenticationRequest设
- 一步步开发自己的博客 番外篇(8、第三方登录及问题记录)
- 各大输入法分类词库内部格式的简单比较
- asp.net-core – 从显式类型的ASP.NET Core API控制器(不是
- 在剃刀mvc 4 rc清空第一行
- asp.net – 我们应该为每个开发人员单独的数据库实例吗?
- 禁用ASP.NET HttpHandler响应缓存
推荐文章
站长推荐
- 在Entity Framework中使用存储过程(三):逻辑删
- asp.net-mvc – 使用URL重写时Url.Action不正确
- asp.net – Razor视图引擎intellisense无法正常工
- asp.net – 在单个位置禁用Windows身份验证
- 文本框导致ASP.NET C#SQL出现问题
- asp.net – 在代码中设置PageSize时,DataPager停
- asp.net-mvc – ASP.NET 4.5在调试模式下捆绑 –
- asp.net – CheckBoxList滚动条
- asp.net – 为什么在“Glimpse Web Debugger”中
- asp.net使用DataTable构造Json字符串的方法
热点阅读