0

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

enter image description here

Xm7X
  • 861
  • 1
  • 11
  • 23
Gayan
  • 2,750
  • 5
  • 47
  • 88
  • Try `routes.Add(new SubdomainRoute())` on `RegisterRoutes` method, you need to include subdomain routing before default routing. Show your current routing scenario if necessary. – Tetsuya Yamamoto Oct 26 '16 at 04:27
  • @TetsuyaYamamoto I have edited question could you please have a look. – Gayan Oct 26 '16 at 05:11
  • Clean your solution first by deleting project's main DLL file (same as project name) if exists. Ensure you have `AreaRegistration.RegisterAllAreas()` method registered on `Application_Start`, then make sure project namespace and project name matches. – Tetsuya Yamamoto Oct 26 '16 at 06:01
  • yeah i did all problem was url match to `admin.domain.com/admin` not `admin.domain.com` how to avoid having area sub from url `/Admin` – Gayan Oct 26 '16 at 06:12
  • To treat `admin` part as subdomain rather than controller part, probably you may try example steps on Tony's blog here: http://blog.tonywilliams.me.uk/asp-net-mvc-2-routing-subdomains-to-areas/. You need to capture host name pattern using `GetVirtualPath` method and redirect to area route using `GetRouteData` there. – Tetsuya Yamamoto Oct 26 '16 at 07:04
  • @TetsuyaYamamoto i have tried that solution as well but no luck – Gayan Oct 26 '16 at 07:10
  • From reading the error looks like you are using the home controller in all your areas, without an overload i cant see this working, Try renaming the Admin area controller to admin and create the routes for it, same for the portal. – pool pro Oct 28 '16 at 06:27

0 Answers0