My requirement is I have the login screen to log in to my site with the url, let's say www.domain.com/login
then logged in user is a admin user route should be admin.domain.com
admin portal implements on separate mvc area if the user is non admin then it should redirect to portal.domain.com
which implement portal area also for non admin users strictly should not be allow to get in to admin area which I can handle with identity.
This Post has nice explanation how to implement sub domain routing. I have tried with this code
public class ExampleRoute : RouteBase
{
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var url = httpContext.Request.Headers["HOST"];
var index = url.IndexOf(".");
if (index < 0)
return null;
var subDomain = url.Substring(0, index);
if (subDomain == "admin")
{
var routeData = new RouteData(this, new MvcRouteHandler());
routeData.Values.Add("controller", "admin"); //Goes to the AdminController class
routeData.Values.Add("action", "Index"); //Goes to the Index action on the AdminController
routeData.DataTokens.Add("area", "Admin");
return routeData;
}
if (subDomain == "portal")
{
var routeData = new RouteData(this, new MvcRouteHandler());
routeData.Values.Add("controller", "portal"); //Goes to the PortalController class
routeData.Values.Add("action", "Index"); //Goes to the Index action on the PortalController
routeData.DataTokens.Add("area", "Portal");
return routeData;
}
return null;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
//Implement your formating Url formating here
return null;
}
}
but i'm not sure how to apply that implementation to my scenario. how to come up with above implementation support to my requirement.
Update I have added the subdomain routing in to the route collection
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.LowercaseUrls = true;
routes.Add(new SubDomainRoute());
routes.MapRoute("Default", "{controller}/{action}/{id}", new
{
controller = "Home",
action = "Index",
id = UrlParameter.Optional
},namespaces: new[] { " SmartMeter.Portal.Controllers" });
}
and Area registered like this
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional },
new[] { "SmartMeter.Portal.Areas.Admin.Controllers" }
);
}
but then I get this error