ASP.NET ASP 2 - 优先考虑路由?
对于一个项目,我有从内容数据库检索的动态页面。然而,某些页面需要一些额外的计算。所以,我想我应该为它们创建一个特定的控制器/视图,并且它们只有在存在时才会被击中,否则,我的动态路由会捕获它,并让内容控制器检索指定路由的数据库内容。 我希望我解释正确,但这里有一些来自我的 Global.asax 的代码可能会进一步解释它:
routes.MapRoute( // Default controller actions, when not found, fall back to next route?
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
routes.MapRoute( // Catch all other? (And try to find content for those)
"DefaultContentRoute",
"{ContentCategory}/{Content}",
new { controller = "Content", action = "Index" },
);
这显然不起作用,因为当我'时,我收到“发现多种类型与名为 xxx 的控制器匹配”错误我为需要额外计算的内容添加了一个控制器。 但我想知道是否还有其他方法可以实现我在这里尝试做的事情? (优先路线) 我显然想让我的 URL 保持完全动态。
非常感谢。
For a project, I'm having dynamic pages that are retrieved from a content database. However, some pages require some additional computing. So, I thought I'd create a specific controller/view for those, and they would only be hit when they exist, otherwise, my dynamic route would catch it, and let the content controller retrieve database content for the specified route.
I hope I explained it right, but here's some code from my Global.asax that might explain it a bit more:
routes.MapRoute( // Default controller actions, when not found, fall back to next route?
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
routes.MapRoute( // Catch all other? (And try to find content for those)
"DefaultContentRoute",
"{ContentCategory}/{Content}",
new { controller = "Content", action = "Index" },
);
This is obviously not working, as I get the "Multiple types were found that match the controller named xxx" error when I'm adding a controller for content that needs extra computing.
But I was wondering if there is any other way to achieve what I am trying to do here? (Prioritizing routes)
I obviously want to keep my URL completely dynamic.
Lot's of thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ASP.NET MVC 会感到困惑,因为任何 URL 都会匹配这两个路由。尝试制定更明确的内容之一,例如:
这将确保所有内容都将通过默认路由,但 URL 中以“类别”开头的内容除外。
另一种方法可能是滥用路由约束,并为 ContentController 路由创建一个约束来检查指定的内容是否存在。
ASP.NET MVC will be confused as any URL will match both routes. Try making one of the more explicit, such as:
This will ensure any content will go through the default route, except for content that starts with "Categories" in the URL.
An alternative may be to abuse route constraints, and create a constraint for your ContentController route that checks if the specified content exists or not.