ASP.NET ASP 2

编程入门 行业动态 更新时间:2024-10-23 05:43:41
本文介绍了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

本文发布于:2023-11-17 04:57:46,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608915.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ASP   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!