【发布时间】:2012-01-01 03:35:56
【问题描述】:
我有一个包含 2 个区域 /Admin 和 /User 的项目。
管理员默认路由是/Admin/Home/Index,用户默认路由是/User/Home/Index。
是否可以实施路由以使其主页 URL 看起来像 /Profile/Index 但为管理员和 /Admin/Home/Index 显示内容strong>/User/Home/Index 对于用户?
更新
终于知道怎么做了
context.MapRoute(
"Admin",
"Profile/{action}",
new { area = AreaName, controller = "Home", action = "Index" },
new { RoleConstraint = new Core.RoleConstraint() },
new[] { "MvcApplication1.Areas.Admin.Controllers" }
);
...
context.MapRoute(
"User",
"Profile/{action}",
new { area = AreaName, controller = "Home", action = "Index" },
new { RoleConstraint = new Core.RoleConstraint() },
new[] { "MvcApplication1.Areas.User.Controllers" }
);
public class RoleConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
string roleName = db.GetRoleByUserName(httpContext.User.Identity.Name);
string areaName = route.Defaults["area"].ToString();
return areaName == roleName;
}
}
它有效,但对我而言,它不是 MVC 方式。有人知道怎么做吗?
【问题讨论】:
标签: asp.net-mvc asp.net-mvc-3 authorization url-routing