c# - ASP.NET MVC 5 Url 重写以包含 Controller 中的所有操作?

标签 c# asp.net asp.net-mvc url-rewriting routes

我想知道是否可以在 Route.Config 文件中编写一个规则,该规则可以包含单个 Controller 中的所有操作?我读过this article但这有点超出我(我刚刚开始使用url重写和路由并且术语不熟悉)。

我已经设法将我的一个操作从 mydomain.co.za/Trainee/Action?id=123 更改为 mydomain.co.za/Trainee/Action/123 但我希望能够包含 Trainee Controller 中的所有操作,这样您就可以有一个规则来生成 Trainee/Action1/123Trainee/Action2/123 这是我使用的代码:

        routes.MapRoute(
            name: "ActionRewrite",
            url: "Trainee/Action/{id}",
            defaults: new { controller = "Trainee", action = "Action" }
        );

附带说明一下,是否也可以在 URL 中隐藏参数,这样无论用户在做什么,您都可以简单地拥有 mydomain.co.za/Action/?

最佳答案

试试这个:

routes.MapRoute(
            name: "ActionRewrite",
            url: "Trainee/{action}/{id}",
            defaults: new { controller = "Trainee", action = {action} }
        );
  • 请记住,您可以使用通配符来匹配您的网址。

  • 如果您愿意,您可以指定 {id} 参数保持可选,这意味着它也将匹配未指定参数的操作。

关于c# - ASP.NET MVC 5 Url 重写以包含 Controller 中的所有操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28078003/

相关文章:

c# - 将按钮图像设置为绑定(bind)其样式

c# - 有人可以在我的例子中解释一下计算 big-O 的逻辑吗

c# - 如何在源代码管理资源管理器 TFS 2012 中强制手动合并?

c# - ASP.Net Core MVC 存储库模式意外处置

javascript - 如何调试嵌入在 View 中的javascript

javascript - 使用 MVC Form Post 发送 HTML 字符串

c# - 添加了在 visual studio 中未检测到的引用

asp.net - 每隔一定时间刷新更新面板

c# - 将 Angular 与现有的 Asp.Net 网站一起使用

c# - 用户代理导致 MVC DisplayFor ArgumentException : Illegal characters in path