没有控制器名称的 MVC 多个默认路由
MVC multiple Default Routes without controller name
我有两个控制器,一个叫 Dashboard,另一个叫 DashboardCash。现在我的应用程序可以被两种类型的用户访问,一种只能访问 Dashboard(A 类用户),而另一种只能访问 DashboardCash(B 类用户)。为了保证我放了一个登录页面。
我想做的是,当 A 类用户成功登录时,我想向他们显示 url 而不是像 http://example.com
这样的控制器名称,而不是像 [=12 这样的控制器名称=].对于 B 类用户,我想向他们展示相同的 http://www.example.com
但在这里我要替换 DashboardCash。
目前我在 Global.asax 文件中定义了这个映射代码:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional
}, // Parameter defaults
new string[] { "Merit.Traveller.BMS.Controllers" });
此代码适用于 Dashboard 现在我想对 DashboardCash 做同样的事情。
编写自定义路由约束,使路由根据用户类型进行匹配。
实现如下接口
public interface IRouteConstraint
{
bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection);
}
然后在路由中使用它,像这样:
routes.MapRoute(name: "newRoute1",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index" },
constraints: new { name = new UserTypeARouteConstraint() }
);
编辑 - 根据您的问题,下面有更多详细信息
这是你的第二条路线的样子
routes.MapRoute(name: "newRoute2",
url: "{controller}/{action}/{id}",
defaults: new { controller = "DashboardCash", action = "Index" },
constraints: new { name = new UserTypeBRouteConstraint() }
);
这就是约束的样子
public class UserTypeARouteConstraint : IRouteConstraint
{
bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return IsUserOfTypeA(httpContext);
}
private bool IsUserOfTypeA(HttpContextbase httpContext)
{
// custom logic to figure out the user group
}
}
谢谢一晒,
这解决了这个问题,我只是添加了一个 EliminateController 作为约束,它允许默认路由到 运行 只有当我们没有仪表板页面时。
详细代码如下:
http://jsfiddle.net/hf7wexkk/ For Code
谢谢。
我有两个控制器,一个叫 Dashboard,另一个叫 DashboardCash。现在我的应用程序可以被两种类型的用户访问,一种只能访问 Dashboard(A 类用户),而另一种只能访问 DashboardCash(B 类用户)。为了保证我放了一个登录页面。
我想做的是,当 A 类用户成功登录时,我想向他们显示 url 而不是像 http://example.com
这样的控制器名称,而不是像 [=12 这样的控制器名称=].对于 B 类用户,我想向他们展示相同的 http://www.example.com
但在这里我要替换 DashboardCash。
目前我在 Global.asax 文件中定义了这个映射代码:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Dashboard", action = "Index", id = UrlParameter.Optional
}, // Parameter defaults
new string[] { "Merit.Traveller.BMS.Controllers" });
此代码适用于 Dashboard 现在我想对 DashboardCash 做同样的事情。
编写自定义路由约束,使路由根据用户类型进行匹配。
实现如下接口
public interface IRouteConstraint
{
bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection);
}
然后在路由中使用它,像这样:
routes.MapRoute(name: "newRoute1",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Dashboard", action = "Index" },
constraints: new { name = new UserTypeARouteConstraint() }
);
编辑 - 根据您的问题,下面有更多详细信息
这是你的第二条路线的样子
routes.MapRoute(name: "newRoute2",
url: "{controller}/{action}/{id}",
defaults: new { controller = "DashboardCash", action = "Index" },
constraints: new { name = new UserTypeBRouteConstraint() }
);
这就是约束的样子
public class UserTypeARouteConstraint : IRouteConstraint
{
bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return IsUserOfTypeA(httpContext);
}
private bool IsUserOfTypeA(HttpContextbase httpContext)
{
// custom logic to figure out the user group
}
}
谢谢一晒,
这解决了这个问题,我只是添加了一个 EliminateController 作为约束,它允许默认路由到 运行 只有当我们没有仪表板页面时。
详细代码如下:
http://jsfiddle.net/hf7wexkk/ For Code
谢谢。