ASP.NET ASP 2-优先路由? [英] ASP.NET ASP 2 - Prioritize Routing?

查看:216
本文介绍了ASP.NET ASP 2-优先路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个项目,我有从内容数据库检索的动态页面.但是,某些页面需要一些额外的计算.因此,我认为我将为这些对象创建一个特定的控制器/视图,并且只有在它们存在时才会被点击,否则,我的动态路由会捕获它,并让内容控制器为指定的路由检索数据库内容.我希望我能正确地解释它,但这是来自Global.asax的一些代码,可能会对其进行更多解释:

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" },  
                );

这显然不起作用,因为当我为需要额外计算的内容添加控制器时,出现发现多个与名为xxx的控制器匹配的错误"错误.但是我想知道是否还有其他方法可以实现我在这里想要做的事情?(优先路线)我显然想保持我的URL完全动态.

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.

非常感谢.

推荐答案

ASP.NET MVC将被混淆,因为任何URL都将同时匹配这两个路由.尝试使其中一个更明确,例如:

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 = "" }   
                ); 

这将确保除URL中以类别"开头的内容以外的所有内容都将通过默认路由.另一种选择是滥用路由约束,并为ContentController路由创建约束,以检查指定内容是否存在.

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.

这篇关于ASP.NET ASP 2-优先路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆