带有子域的路由

发布于 2024-09-06 08:04:15 字数 380 浏览 5 评论 0原文

我有一个 MVC 网站,它有 3 个主要组件

  1. 成员区域,路径为 /Member/{controller}/{action}/{id}
  2. 单个页面将响应任何子域,例如 user1.example.com/{controller}/ {action}/{id}, user2.example.com/{controller}/{action}/{id}
  3. 将响应 www.example.com/{controller}/{action}/{id 下任何网址的主网站 ?

处理允许上述 3 项共存的路由的最简单方法是什么 我已经尝试了 global.asax.cs 文件中的许多 MapRoutes,并且还基于 RouteBase 创建了一个新类,但运气不佳。

I have an MVC website which has 3 main components

  1. Member area which has the path /Member/{controller}/{action}/{id}
  2. Individual pages which will respond to any subdomain, e.g. user1.example.com/{controller}/{action}/{id}, user2.example.com/{controller}/{action}/{id}
  3. Main website which will respond to any url under www.example.com/{controller}/{action}/{id}

What is the easiest way to handle the routes that will allow the above 3 items to co-exist? I have tried many MapRoutes from the global.asax.cs file and also making a new class based on RouteBase but not having much luck.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2024-09-13 08:04:15

听起来您正朝着正确的方向前进 - 本质上您需要创建一个自定义路由来查看请求并构造路由值字典。不过,有人已经创建了一个很好的实现,它允许您在域本身中包含占位符,而不是重新发明轮子,如下所示:

routes.Add("DomainRoute", new  DomainRoute(
"{controller}.example.com",  "{action}/{id}",
new { controller = "Home", action = "Index", id = "" }));

http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx

It sounds like you're heading in the right direction - essentially you need to create a custom route which looks at the request and constructs the route value dictionary. Rather than reinvent the wheel though, someone has already created a nice implementation which allows you to include placeholders in the domain itself like so:

routes.Add("DomainRoute", new  DomainRoute(
"{controller}.example.com",  "{action}/{id}",
new { controller = "Home", action = "Index", id = "" }));

http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文