ASP.NET MVC 2.0 中的路由
我希望在我的 ASP.NET MVC 2.0 网站中创建一条非常简单的路线。我一直在谷歌上寻求帮助,但我能找到的所有示例都是针对非常复杂的路由的。
基本上我希望主控制器中的所有页面都在域之后解析,而不是 /Home/
例如我想要 http://www.MyWebsite.com/Home/LandingPage/
成为http:// /www.MyWebsite.com/LandingPage/
但仅限于主控制器,我希望其余控制器正常工作。
我考虑过为每个控制器创建一个控制器并仅使用一个索引,但是我们需要大量的登陆页面来进行这样的营销,这会很快使站点加载每个页面的控制器,这不太理想。
I'm looking to make a really simple route in my ASP.NET MVC 2.0 website. I've been googling for help but all the examples I can find are for really complex routing.
Basically I want all the pages in my Home Controller to resolve after the domain as opposed to /Home/
For example I want http://www.MyWebsite.com/Home/LandingPage/
To become http://www.MyWebsite.com/LandingPage/
But only for the Home controller, I want the rest of my controllers to function as normal.
I thought about creating a controller for each and just using an index, but we need lots of landing pages for our marketing like this and it would quickly make the site loaded with controllers for a single page each, which is less than ideal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实现此目的的一种方法是为每个着陆页设置单独的路线。另一种方法是使用一条具有与每个着陆页(而不是其他任何内容)匹配的约束的路线。
请注意,您也可以通过一些反思来做到这一点(未经测试)。
RouteConstraint 解决方案类似,只是您有一个带有自定义约束的路由,该路由评估适当的路由值是否与 HomeController 上的方法之一匹配,如果匹配,则将控制器和操作替换为“home”,匹配值。
请注意,即使使用反射,每页路由机制也仅执行一次。从那时起,您每次都会进行简单的查找。 RouteConstraint 机制每次都会使用反射来查看路由是否匹配(除非它缓存结果,我认为它不会缓存结果)。
One way to do this would be to have a separate route for each landing page. Another way would be to have a single route with a constraint that matches each landing page (and nothing else).
Note that you could probably do this with a bit of reflection as well (untested).
The RouteConstraint solution would be similar except that you'd have a single route with a custom constraint that evaluated whether the appropriate route value matched one of the methods on the HomeController and, if so, replaced the controller and action with "home" and the matched value.
Note that the route per page mechanism, even if you use reflection, is done only once. From then on you do a simple look up each time. The RouteConstraint mechanism will use reflection each time to see if the route matches (unless it caches the results, which I don't think it does).
我认为您缺少默认路由。
因此,当您输入 www.mywebsite.com 时,控制器、操作和 id 参数将具有以下值:
I think you are missing the default route.
So, when you type www.mywebsite.com, the controller, action, and id parameters would have the following values: