I have a project with 2 areas /Admin and /User.
Admin's default route is /Admin/Home/Index and user's default route is /User/Home/Index.
Is it possible to implement routing to make their home URL to look like /Profile/Index but to show content from /Admin/Home/Index for admins and /User/Home/Index for users?
upd
Finally find out how to do it
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;
}
}
It works, but as for me it's not the MVC way. Does anybody knows how to do it right?