C# Route始终指向第一个maproute

C# Route始终指向第一个maproute,c#,asp.net-mvc,asp.net-mvc-2,routing,asp.net-mvc-routing,C#,Asp.net Mvc,Asp.net Mvc 2,Routing,Asp.net Mvc Routing,我正在尝试创建类似以下内容的URI: http://hostname/mobile/en/controller/action适用于手机或http://hostname/en/controller/action用于桌面(非手机) 我的路由表当前看起来像这样(Global.asax.cs) 当我试着做一个测试时,问题就出现了 返回重定向到操作(“添加”、“用户”) 当我想让桌面浏览器转到/en/User/Add时,它总是将桌面浏览器从/en/User/List重定向到/mobile/en/User/

我正在尝试创建类似以下内容的URI:
http://hostname/mobile/en/controller/action
适用于手机或
http://hostname/en/controller/action
用于桌面(非手机)

我的路由表当前看起来像这样(Global.asax.cs)

当我试着做一个测试时,问题就出现了
返回重定向到操作(“添加”、“用户”)
当我想让桌面浏览器转到
/en/User/Add
时,它总是将桌面浏览器从
/en/User/List
重定向到
/mobile/en/User/Add
。 移动版本工作正常,但我相信这是因为第一条“移动”路线始终被视为匹配的路线,即使它在开始时没有/Mobile/也不例外

我尝试在两个版本中使用同一个控制器,但始终无法重定向到移动路径。这意味着
RedirectToRoute
不是完美的,因为我需要动态路由


谢谢你的帮助。

嘿,你可能想看看这篇文章中的分组控制器:


希望对你有帮助。我们在工作中使用它,并且进展顺利

主要问题是您提供的路由值与两条路由匹配,这意味着它将使用第一条路由(移动)

您可以通过重定向到路由而不是操作来手动选择要使用的路由

return RedirectToRoute("Mobile", new {
    language = "en", controller = "User", action = "Get", id = 20
});
移动或

return RedirectToRoute("Default", new {
    language = "en", controller = "User", action = "Get", id = 20
});
为您的默认路线

但是,这给您带来了一个新问题:获取当前路由的名称。如示例所示,引用命名路由是非常可能的。然而,获取当前使用的路由的名称似乎是不可能的。使用类似黑客的方式查看当前URI显然是不可取的。除了丑陋(黑客),它还可能导致大量代码重复

但是,可以向路由添加值,这些值可以从控制器(或视图)轻松获得:

现在,通过代码,您可以使用路由本身提供的值重定向到当前路由:

return RedirectToRoute(RouteData.Values["routeName"], new {
    language = "en", controller = "User", action = "Get", id = 20
});
现在,如果您想让这完全成为一种幻想,您甚至可以从中创建一个扩展方法:

public static class ControllerExtensions {
    public static RedirectToRouteResult CustomRedirectToRoute(this Controller controller, string controllerName, string actionName, object routevalues) {
        return CustomRedirectToRoute(controller, controllerName, actionName, new RouteValueDictionary(routevalues));
    }

    public static RedirectToRouteResult CustomRedirectToRoute(this Controller controller, string controllerName, string actionName, RouteValueDictionary routevalues) {
        routevalues = routevalues ?? new RouteValueDictionary();
        routevalues.Add("controller", controllerName);
        routevalues.Add("action", actionName);
        return new RedirectToRouteResult(controller.RouteData.Values["routeName"] as string, routevalues);
    }
}

希望这能为您的问题提供一个很好的解决方案

谢谢勒内,这看起来正是我希望做的。你能告诉我把扩展方法放在哪里最好吗?把静态类ControllerExtensions放在你想要的任何地方。在要使用重定向的控制器中,确保包含extensions类所在的命名空间。然后,您可以在控制器中的任何位置使用这个.CusomRedirectToRoute(“主”、“索引”、路由值);如果您愿意,您还可以添加另一个扩展方法,将null作为routeValues值。如果您想整理它,我的评论的后半部分可以替换为用于获取路由名称的整洁系统,如本文所述:感谢链接。如果我扩展项目,那就需要考虑一下。
return RedirectToRoute(RouteData.Values["routeName"], new {
    language = "en", controller = "User", action = "Get", id = 20
});
public static class ControllerExtensions {
    public static RedirectToRouteResult CustomRedirectToRoute(this Controller controller, string controllerName, string actionName, object routevalues) {
        return CustomRedirectToRoute(controller, controllerName, actionName, new RouteValueDictionary(routevalues));
    }

    public static RedirectToRouteResult CustomRedirectToRoute(this Controller controller, string controllerName, string actionName, RouteValueDictionary routevalues) {
        routevalues = routevalues ?? new RouteValueDictionary();
        routevalues.Add("controller", controllerName);
        routevalues.Add("action", actionName);
        return new RedirectToRouteResult(controller.RouteData.Values["routeName"] as string, routevalues);
    }
}