两种不同的通用路由类型
我的 ASP.NET MVC 应用程序中有两种(到目前为止)不同类型的路由,一种是:{controller}/{action}/{id},另一种是 {controller}/{action}/{title}
目前我需要像这样定义路由:
routes.MapRoute (
"Default_Title_Slug", // Route name
"product/details/{title}", // URL with parameters
new { controller = "product", action = "details", title = "" } // Parameter defaults
);
routes.MapRoute (
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "site", action = "index", id = "" } // Parameter defaults
);
请注意,我必须将第一个路由绑定到产品控制器,这似乎是我可以让它工作的唯一方法...否则其他路由最终看起来像这样:
/controller/action?id=number
现在我需要添加另一个针对具有 {title} 段的另一个控制器的 MapRoute 调用...我不想为将来提出的每个特定条目创建新路线...是否有通用路线我可以创建映射 /controller/action/title ,以便与 /controller/action/id 路由很好地配合?
谢谢,
基隆
I've got two (so far) different types of routes in my ASP.NET MVC app, one is: {controller}/{action}/{id} and the other {controller}/{action}/{title}
Currently I need to define the routes like this:
routes.MapRoute (
"Default_Title_Slug", // Route name
"product/details/{title}", // URL with parameters
new { controller = "product", action = "details", title = "" } // Parameter defaults
);
routes.MapRoute (
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "site", action = "index", id = "" } // Parameter defaults
);
Notice that the first one I've had to tie down to the product controller, this seems to be the only way I can get it work...otherwise the other routes end up looking like this:
/controller/action?id=number
Now I need to add another MapRoute call targeting another controller with the {title} segment...I don't want to create a new route for each specific entry I come up with in the future...is there a generic route I can create to map the /controller/action/title that'll play nicely with the /controller/action/id route?
Thanks,
Kieron
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用路由约束(例如正则表达式)来做到这一点 - 一个非常类似的示例是 此处。像这样的东西:
You can do that with a route-constraint, such as regex - a very similar example is here. Something like: