Subdomain Mapping in ASP.NET Boilerplate

In AbpTenants table in the database. You can add your custom properties to Tenant class.
AbpTenant class defines some basic properties, the most important properties are:

  • TenancyName: This is a unique name of a tenant in the application. It should not be changed normally. It can be used to allocate subdomains to tenants like ''. Tenant.TenancyNameRegex constant defines the naming rule.
  • Name: An arbitrary, human-readable, long name of the tenant.
  • IsActive: True, if this tenant can use the application. If it's false, no user of this tenant can login to the system.

Please find below the code segments that do this:

// Part 1 - Initialize cache (or inject depending on scenario)

ITypedCache<string, Tenant> _tenantCache;

using (var cacheManager = AbpBootstrapper.IocManager.ResolveAsDisposable<CacheManagerBase>())
    _tenantCache = cacheManager.Object.GetCache<string, Tenant>("TenantCache");			
// Part 2 - Detect subdomain & look up tenant
if (Request.Url.HostNameType == UriHostNameType.Dns)						
    // Try to get tenant name from sub-domain
    string tenantName = Request.Url.Host.Split('.')[0];						

    Tenant t = _tenantCache.Get(tenantName,							
        () => 											
            // attempt to look up									
            return AsyncContext.Run<Tenant>(() => GetTenant(tenantName));				
// Part 3 - Set the current tenant

    if (t != null)
        // Set tenant ID										  											
private async Task<Tenant> GetTenant(string tenantName)
    TenantManager manager = AbpBootstrapper.IocManager.Resolve<TenantManager>();		
    Tenant t = await manager.FindByTenancyNameAsync(tenantName);				
    return t;											

Share this post


  • Bert Sinnema Reply

    Hi, This looks promising but the post is not very helpful. It would be awesome if you could transform this somehow into a tutorial, would be great. I'm not sure where to locate the file and what exactly is changing and why. Also I would like to know what behavioral changes there really are. Cheers!

    Monday, January 09, 2017 at 04:56 AM
    • Amr Saafan Reply

      Nice to hear from you , sure we need more detailed tutorial , but we could say that it an inspiration or starting point All the questions you have asked need to have been answered clarified as well

      Friday, January 20, 2017 at 08:03 AM

Leave a comment

Previous Post

Next Post