且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

带有默认控制器的 ASP.NET MVC 路由

更新时间:2023-02-15 20:48:21

不要将诸如 "{action}/{id}" 之类的规则与 "{controller}/{action}/{id}" ... 特别是当后面的 id 有一个默认值 ie 是可选的.

don't mix a rule like: "{action}/{id}" with one that's "{controller}/{action}/{id}" ... specially when id in the later has a default value i.e. is optional.

在这种情况下,您没有任何东西可以让路由知道使用哪个是正确的.

In that case you have nothing that allows routing to know which one is the right one to use.

如果这是您需要的解决方法,那就是添加约束(请参阅 this) 到前面的动作到一组值即列表、视图.当然,对于这些类型的规则,您不能拥有与操作名称相同的控制器.

A workaround, if that's what you need, would be to add a constrain (see this) to the action in the earlier to a set of values i.e. List, View. Of course that with these types of rules, you can't have a controller with the same name of an action.

还请记住,如果您指定默认操作 &"{action}/{id}" 规则中的 id,当你点击你网站的路由时会用到.

Also remember that if you specify a default action & id in the "{action}/{id}" rule, that will be used when you hit the route of your site.