2011-01-10 83 views
1

对于一个项目,我拥有从内容数据库中检索的动态页面。但是,有些页面需要额外的计算。所以,我想我会为这些创建一个特定的控制器/视图,并且只有当它们存在时才会被触发,否则,我的动态路由会捕获它,并让内容控制器检索指定路由的数据库内容。 我希望我解释是正确的,但在这里是从我的Global.asax一些代码,可以解释它多一点:ASP.NET ASP 2 - 优先路由?

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“错误,当我为需要额外计算的内容添加控制器时。 但我想知道是否有其他方法可以实现我在这里要做的事情? (优先化路线) 我显然希望保持我的网址完全动态。

很多人的感谢提前。

回答

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

+0

我认为滥用路线限制对我来说可能是最好的解决方案。在我们说话时试验它。 (或者,嘿嘿) 非常感谢。 – DmY 2011-01-10 14:46:59