Search code examples
asp.net-coreasp.net-core-mvc

Mapping Area to Subdomain ASP.NET Core 7


I'm trying to Map areas I've created to subdomains and failing.

I have an area named "sugar" and I'm using a SubDomainRouteTransformer class I found online that worked for an ASP.NET core 5 project with the code:


    

    public class SubDomainRouteTransformer : DynamicRouteValueTransformer
    {
        public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
        {
            var subDomain = httpContext.Request.Host.Host.Split(".").First();
            if (string.IsNullOrEmpty(subDomain))
            {
                values["controller"] = subDomain;
            }
            return values;
        }
    }

in my program.cs file I implement it thus

    builder.Services.TryAddTransient<SubDomainRouteTransformer>();

    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    
    app.MapDynamicControllerRoute<SubDomainRouteTransformer>("Sugar", "{area:exists}/{controller=Home}/{action=Index}/{id?}");

However, whenever I try accessing the main website, which is the one under the "default" route, I keep getting directed to the "sugar" area. Is there a proper way I can implement this?


Solution

  • Firstly,with your codes,"Sugar" would be considered as the route parttern,you have to remove "Sugar"

    enter image description here

    Secondly

    whenever I try accessing the main website, which is the one under the "default" route, I keep getting directed to the "sugar" area

    check if subdomain exists:

    var host = httpContext.Request.Host.Host;
    var subDomain = host.Split(".").Count() > 1 ? host.Split(".").First() : "";
    

    If you don't want to be directed to Sugar area when subdomain doesn't exists,try

    if (!string.IsNullOrEmpty(subDomain))
    {
      values["controller"] = subDomain;
      values["area"] = subDomain;
    }
    else
    {
       values.Remove("area");
     }
    

    I tried with a minimal example:

    enter image description here

    enter image description here

    in program.cs:

    app.MapDynamicControllerRoute<SubDomainRouteTransformer>("{area}/{controller=Home}/{action=Index}/{id?}");
    
    app.MapControllerRoute(
        name: "default",
        pattern: "{controller=Home}/{action=Index}/{id?}");
    

    SubDomainRouteTransformer:

    public class SubDomainRouteTransformer : DynamicRouteValueTransformer
        {
            public override async ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
            {
                var host = httpContext.Request.Host.Host;
                var subDomain = host.Split(".").Count() > 1 ? host.Split(".").First() : "";
                if (!string.IsNullOrEmpty(subDomain))
                {
                    values["controller"] = subDomain;
                    values["area"] = subDomain;
                }
                else
                {
                    values.Remove("area");
                }
                return values;
            }
        }
    

    Result:

    enter image description here

    enter image description here