Skip to content

Multitenancy in subdomain

Mariusz Kerl edited this page Jan 30, 2018 · 3 revisions

Creating subdomain route is really easy, but needs some explanation first. Domain can be almost anything nowdays. Because of that we will have to create a list containing all known hosts for our application, at least those which we wan't to be recognized by our app. This will allow to correctly catch routes and generate links. For now let's just use application host value. For me it is: localhost:54575.

public void Configure(IApplicationBuilder app)
{
    //...
    app.UseMvc(routes =>
    {
        var hostnames = new[] { "localhost:54575" };

        routes.MapSubdomainRoute(
            hostnames,
            "TenantInSubdomain",
            "{tenant}",
            "{controller}/{action}",
            new { controller = "Home", action = "Index" });
    });
}

Pretty simple right? Let's explain line by line what's happening. First we define our hostnames. MapSubdomainRoute takes those hostnames as first parameter. Second parameter is route name, third is template for subdomain, fourth parameter is virtual path template, last is default values.