asp.net-mvc-3 - 如何简化 MVC3 路由?

标签 asp.net-mvc-3 asp.net-mvc-routing

我有以下路线。我想我可以简化它们,但我不确定如何简化。有人可以给我一些建议吗?这些 route id = ... 的原因是什么。如果我的方法中没有任何 id 参数,那么这有什么作用吗?

context.MapRoute(
    "Admin_test",
    "Admin/Tests",
    new { controller = "Contents", action = "Tests", id = UrlParameter.Optional }
);
context.MapRoute(
    "Admin_menus",
    "Admin/Menus",
    new { controller = "Contents", action = "Menus", id = UrlParameter.Optional }
);
context.MapRoute(
    "Admin_notes",
    "Admin/Pages",
    new { controller = "Contents", action = "Pages", id = UrlParameter.Optional }
);
context.MapRoute(
    "Admin_cores",
    "Admin/Cores",
    new { controller = "Cores", action = "Cores", id = UrlParameter.Optional }
);
context.MapRoute(
    "Admin_default3",
    "Admin/References",
    new { controller = "References", action = "References", id =     UrlParameter.Optional }
);

最佳答案

context.MapRoute(
    "Admin_Contents",
    "Admin/{action}",
    new { controller = "Contents" },
    new { action = "^tests|menus|pages$" }
);

context.MapRoute(
    "Admin_Default",
    "Admin/{controller}",
    new { action = "Index" }
);

然后在您的 CoresReferences Controller 上使用更多 RESTful 操作命名约定,并使用 Index 作为默认操作而不是 核心引用

但是在第二个路由定义中,您可能还希望允许用户在 url 中指定不同的操作:

context.MapRoute(
    "Admin_Default",
    "Admin/{controller}/{action}",
    new { action = "Index" }
);

关于asp.net-mvc-3 - 如何简化 MVC3 路由?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11329775/

相关文章:

asp.net-mvc - 阻止 Google Analytics 在开发环境、ASP.NET MVC 中收集数据

c# - 如何检查现有记录

c# - 如何实现 Telerik 自定义聚合?

asp.net-mvc - 如何在ASP.NET MVC中添加到动态robots.txt的路由?

asp.net-mvc - ASP.NET MVC路由上的斜杠

asp.net-mvc-routing - 如何检查给定路径是否存在路由(ASP.NET MVC)?

js 文件中的 jQuery ajax

c# - ASP.NET MVC 3 中注销时清理浏览器缓存和 cookie 的问题

asp.net-mvc-3 - ASP.NET MVC 默认路由可通过区域路由访问

c# - MVC 映射 - RedirectToAction 不起作用