ASP.NET ASP 2 - 优先考虑路由?

发布于 2024-10-11 09:30:03 字数 973 浏览 2 评论 0原文

对于一个项目,我有从内容数据库检索的动态页面。然而,某些页面需要一些额外的计算。所以,我想我应该为它们创建一个特定的控制器/视图,并且它们只有在存在时才会被击中,否则,我的动态路由会捕获它,并让内容控制器检索指定路由的数据库内容。 我希望我解释正确,但这里有一些来自我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下慵懒的猫 2024-10-18 09:30:03

ASP.NET MVC 会感到困惑,因为任何 URL 都会匹配这两个路由。尝试制定更明确的内容之一,例如:

routes.MapRoute( // Catch all other? (And try to find content for those) 
                    "DefaultContentRoute",               
                    "Categories/{ContentCategory}/{Content}",                         
                    new { controller = "Content", action = "Index" },   
                ); 

routes.MapRoute( // Default controller actions, when not found, fall back to next route? 
                    "Default",                                               
                    "{controller}/{action}/{id}",                            
                    new { controller = "Home", action = "Index", id = "" }   
                ); 

这将确保所有内容都将通过默认路由,但 URL 中以“类别”开头的内容除外。
另一种方法可能是滥用路由约束,并为 ContentController 路由创建一个约束来检查指定的内容是否存在。

ASP.NET MVC will be confused as any URL will match both routes. Try making one of the more explicit, such as:

routes.MapRoute( // Catch all other? (And try to find content for those) 
                    "DefaultContentRoute",               
                    "Categories/{ContentCategory}/{Content}",                         
                    new { controller = "Content", action = "Index" },   
                ); 

routes.MapRoute( // Default controller actions, when not found, fall back to next route? 
                    "Default",                                               
                    "{controller}/{action}/{id}",                            
                    new { controller = "Home", action = "Index", id = "" }   
                ); 

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文