.NET MVC 3 路由
最近,我从 Web 窗体过渡到 MVC 3,并且一直在尝试掌握 MVC 路线。我有一个有点特殊的问题,当我收到对我的应用程序的请求(例如 subdomain1.organization.com 或 subdomain2.organization.com)时,我希望按如下方式使用默认路由:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
但是,当我的应用程序收到请求时通过特定子域(例如 subdomain3.organization.com)的应用程序,我希望应用程序默认为特定控制器。我一直在关注代码:
http:// /blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx
这应该是我想要的。因此,我的 Global.asax 中的代码如下:
routes.Add("DomainRoute", new DomainRoute(
"subdomain3.organisation.com", // Domain with parameters
"{action}/{id}", // URL with parameters
new { controller = "Subdomain3Controller", action = "Index", id = "" }
));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
部署后,当使用 Subdomain3Controller 将请求发送到 subdomain3.organization.com 时,我的应用程序行为正确。但是,当访问任何其他子域(例如 localhost/Subdomain3Controller/Index)时,我的应用程序似乎选择了错误的路线。
我的表单助手似乎返回了错误的值:
ViewContext.Controller.ValueProvider.GetValue("controller").AttemptedValue
@using(Html.BeginForm(ViewContext.Controller.ValueProvider.GetValue("action").AttemptedValue, ViewContext.Controller.ValueProvider.GetValue("controller").AttemptedValue, FormMethod.Post, new Dictionary<string, object> {{ "id", "formid" } })){
有什么想法吗?任何人都可以就这个问题提供任何线索,我们将不胜感激。
非常感谢。
Recently I've made the transition from Web Forms to MVC 3 and I've been trying to get to grips with MVC routes. I have a somewhat peculiar problem in that when I receive a request to my application (e.g. subdomain1.organisation.com or subdomain2.organisation.com) I wish the default route to be used as follows:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
However, when a request is received by my application through a particular subdomain e.g. subdomain3.organisation.com, I want the application to default to a particular controller. I've been following code at:
http://blog.maartenballiauw.be/post/2009/05/20/ASPNET-MVC-Domain-Routing.aspx
which should what I want. So the code in my Global.asax is as follows:
routes.Add("DomainRoute", new DomainRoute(
"subdomain3.organisation.com", // Domain with parameters
"{action}/{id}", // URL with parameters
new { controller = "Subdomain3Controller", action = "Index", id = "" }
));
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
When deployed, my application behaves correctly when requests are sent to subdomain3.organisation.com, using Subdomain3Controller. However, when visiting any other subdomain e.g. localhost/Subdomain3Controller/Index my application seems to select the incorrect route.
My form helpers appear to return an incorrect value for:
ViewContext.Controller.ValueProvider.GetValue("controller").AttemptedValue
@using(Html.BeginForm(ViewContext.Controller.ValueProvider.GetValue("action").AttemptedValue, ViewContext.Controller.ValueProvider.GetValue("controller").AttemptedValue, FormMethod.Post, new Dictionary<string, object> {{ "id", "formid" } })){
Any ideas why this might be? Any light that anyone could shed on this issue would be much appreciated.
Many thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过路由调试器,它旨在帮助调试此类问题
看起来现在也可以通过 Nuget 安装。
Have you tried the routing debugger, which is designed to help debug these sorts of problems
Looks like it's installable via Nuget now too.
我还注意到,如果我将控制器名称硬编码到 BeginForm 帮助程序中,仍然会返回不正确的操作值:
生成
:而不是:
在查看时
http://localhost/Subdomain3Controller/ 或 http://localhost/Subdomain3Controller/Index
I've also noticed that if I hard-code the controller name into the BeginForm helper, the incorrect action value is still returned:
generates:
rather than:
when viewing:
http://localhost/Subdomain3Controller/ or http://localhost/Subdomain3Controller/Index
偶然发现了类似的问题并设法找到了该问题的解决方案。我知道这可能对原作者没有帮助,但它可能会对通过搜索最终到达这里的其他一些人有所帮助。
不管怎样,看起来“Default”这个路线名称并没有增加太多的重量。当 BeginForm 构建其 URL 时,它会从路由集合中获取第一个“适合”的路由。我没有深入研究这一切的内部运作方式。
我为解决这个问题所做的就是向子域路由添加一个约束,使其只能用于 Subdomain3Controller,如下所示:
[编辑]
经过更多思考,发现我的情况与原始发帖人的情况有点不同。上面的方法不起作用,因为控制器不再位于 URL 内部。
解决方案可能是切换路由的顺序,并将子域的 NotEqual 约束添加到默认路由。如 中所述http://stephenwalther.com/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx
[/编辑]
Stumbled on a similar problem and managed to find a solution for this problem. I'm aware this might not help the original author, but it might help some other people who end up here via search.
Anyway, it seems the route name "Default" doesn't add much weight. When BeginForm is constructing its URL it takes the first route from the route collection that 'fits'. I didn't dig too deep into how this all works internally.
What I did to solve this problem was add a constraint to the subdomain route so it can only be used for the Subdomain3Controller, as such:
[edit]
After thinking about this a little bit more and realizing my situation was a bit different from the situation of the original poster. The above won't work, because the controller is not inside of the URL anymore.
A solution might be to switch the order of the routes, and add a NotEqual constraint for the subdomains to the default route. As described in http://stephenwalther.com/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx
[/edit]