【问题标题】:ASP.NET ASP 2 - Prioritize Routing?ASP.NET ASP 2 - 优先路由?
【发布时间】:2011-06-06 13:54:54
【问题描述】:

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

非常感谢。

【问题讨论】:

    标签: asp.net asp.net-mvc-2 routing


    【解决方案1】:

    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 路由创建一个约束,以检查指定的内容是否存在。

    【讨论】:

    • 我认为滥用路线限制可能是我的最佳解决方案。在我们说话的时候试验它。 (或者,输入 hehe)非常感谢。
    • 请注意,您可能希望在此处进行一些缓存,因为如果检查每个请求,可能会导致数据库负载过大...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-13
    • 2014-05-23
    • 1970-01-01
    相关资源
    最近更新 更多