我想知道是否可以在 Route.Config 文件中编写一个规则,该规则可以包含单个 Controller 中的所有操作?我读过this article但这有点超出我(我刚刚开始使用url重写和路由并且术语不熟悉)。
我已经设法将我的一个操作从 mydomain.co.za/Trainee/Action?id=123
更改为 mydomain.co.za/Trainee/Action/123
但我希望能够包含 Trainee Controller 中的所有操作,这样您就可以有一个规则来生成 Trainee/Action1/123
或 Trainee/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/