asp.net-mvc – MVC5中的默认控制器和默认操作
发布时间:2020-12-16 06:39:38 所属栏目:asp.Net 来源:网络整理
导读:我有一个在MVC 5中开发的网站,我正在使用路由属性进行路由. 我使用以下代码为每个控制器设置了默认控制器和默认操作 public class CompanyController : MainController { [Route("~/",Name = "default")] [Route("Company/Index")] public ActionResult Inde
我有一个在MVC 5中开发的网站,我正在使用路由属性进行路由.
我使用以下代码为每个控制器设置了默认控制器和默认操作 public class CompanyController : MainController { [Route("~/",Name = "default")] [Route("Company/Index")] public ActionResult Index(string filter = null) { //My code here } [Route("Company/Edit")] public ActionResult Edit(int id) { //My code here } } 我有一个默认操作的另一个控制器: [RoutePrefix("Analyst")] [Route("{action=Index}")] public class AnalystController : MainController { [Route("Analyst/Index")] public ActionResult Index(string filter = null) { //My code here } [Route("Analyst/Edit")] public ActionResult Edit(int id) { //My code here } } 默认控制器运行正常,但是当我导航到分析器控制器而未指定操作的名称时,我收到以下错误: Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL. The request has found the following matching controller types: SurveyWebsite.Controllers.AnalystController SurveyWebsite.Controllers.CompanyController 如何更正导航到http://localhost:61534/analyst并达到默认操作(索引)?该行动也应在http://localhost:61534/analyst/Index之前保持可用 解决方法
给出一个空字符串作为索引操作的路由值,以便它适用于Analyst,这是您的控制器路由前缀.您可以使用第二个Route属性进行装饰,以使其与“Analyst / Index”URL一起使用,您可以在其中传递“Index”.
[RoutePrefix("Analyst")] public class AnalystController : MainController { [Route("")] [Route("Index")] public ActionResult Index(string filter = null) { //My code here } [Route("Edit/{id}")] public ActionResult Edit(int id) { //My code here } } 这适用于/ Analyst和/ Analyst / Index (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 具有任意数量的键值对的ASP.NET路由 – 是否可能?
- asp.net – 如何以编程方式将位置元素添加到Web配置?
- [ASP.NET MVC]为HtmlHelper添加一个RadioButtonList扩展方法
- 在ASP.NET中动态设置元素属性的值
- asp.net-mvc – 将返回文件的长时间运行进程
- asp.net-mvc – 如何设置内联的webgrid行样式
- Asp.NET Core2.0 项目实战入门视频课程_完整版
- asp.net-mvc – 如何在WEB API 2中创建异步验证属性
- asp.net-mvc – IIS通过http方法重写排除规则
- [asp.net core 源码分析] 01 - Session
推荐文章
站长推荐
- asp.net-mvc – 如何在ASP.NET MVC 2 RC中编写自
- 使用Azure ACS时无法从Active Directory注销
- asp.net-mvc – 部署后不显示捆绑的css … ASP.N
- asp.net – 从ASP Classic迁移到.NET并减轻疼痛
- asp.net – 为什么在设置@Page masterPageFIle时
- asp.net-mvc – 当绑定到ViewModel时如何更新模型
- asp.net-mvc-3 – 与SQL Server建立连接时发生与
- ASP.NET MVC+EF 项目架构搭建
- 从asp.net代码中调试VS 2008中的sql存储过程
- asp.net-mvc – ASP.net MVC 4 WebApi中的嵌套资
热点阅读