Dmi*_*rij 17 asp.net-mvc authorization url-routing asp.net-mvc-3
我有一个项目有2个区域/管理员和/用户.
管理员的默认路由是/ Admin/Home/Index,用户的默认路由是/ User/Home/Index.
是否可以实现路由以使其主页URL看起来像/ Profile/Index,但是显示来自/ Admin/Home/Index for admins和/ User/Home/Index for users的内容?
UPD
最后找出如何做到这一点
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;
}
}
Run Code Online (Sandbox Code Playgroud)
它有效,但对我而言,它不是MVC的方式.有谁知道怎么做对吗?
是的。您展示的示例与 Microsoft 提供的许多使用路由约束的示例非常接近。在请求传递到控件之前,路由引擎充当预代理(或路由器,如果您愿意的话)。像 IRouteConstraint 这样的项目已被定义,因此您可以按照您所描述的进行操作。
归档时间: |
|
查看次数: |
4488 次 |
最近记录: |