Tôi có một dự án với 2 khu vực/Quản trị viên và/Người dùng.Định tuyến dựa trên vai trò MVC
tuyến đường mặc định của Admin là /Admin/Home/Index và tuyến đường mặc định của người dùng là /User/Home/Index.
Có thể thực hiện định tuyến để làm cho URL trang chủ của họ để trông giống như /Thông tin/Index nhưng để hiển thị nội dung từ /Admin/Home/Index cho quản trị viên và /User/Home/Index cho người dùng ?
UPD
Cuối cùng tìm hiểu làm thế nào để làm điều đó
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;
}
}
Nó hoạt động, nhưng như đối với tôi nó không phải là cách MVC. Có ai biết làm thế nào để làm điều đó đúng không?