Reputation: 55
I have an Asp.net MVC3 application I want to be able to allow multiple/ different clients to access the same application but using different url's. I have already managed to configure the database to allow this. So hia's the main part i want to host my application in a domain say... www.myapplication.com then allow different client to access the same application using 1.www.clientOne.myapplication.com 2.www.clientTwo.myapplication.com.
How to do it? Please provide the code. Thanks.
Upvotes: 3
Views: 5522
Reputation: 3143
To implement multi tenant application you have to do some tricks in
C:\Windows\System32\drivers\etc host file
and some coding tricks in App_Start folder at RouteConfig.Cs
file by inheriting and implementing IRouteConstraint
interface. Also need to see bindings settings in IIS server(for this you need to see the video tutorial provided in below link)
You can get a complete code here https://github.com/ashikcse20/ASP-MVC5-MULTI-TENANT-REPOSITORY with complete written tutorial.
The video tutorial is given here ASP .NET MVC 5 Multi Tenant Example With Basic Code (Single Database Per Tenant)
Code in RouteConfig.Cs
at RegisterRoutes function
routes.MapRoute(
name: "Default", url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { TenantRouting = new RoutingConstraint() }
);
Inheriting and implementing IRouteConstraint Interface
public class RoutingConstraint : IRouteConstraint // It is main Class for Multi teanant
{
public bool Match(HttpContextBase httpContext, Route route, string getParameter, RouteValueDictionary values, RouteDirection routeDirection)
{
// Got htis code from http://blog.gaxion.com/2017/05/how-to-implement-multi-tenancy-with.html
var GetAddress = httpContext.Request.Headers["Host"].Split('.');
var tenant = GetAddress[0];
//Here you can apply your tricks and logic. Note for when you put it in public server then www.hamdunsoft.com , www.tenant1.hamdunsoft.com then you need to change a little bit in the conditions . Because a www. was added.
if (GetAddress.Length < 2) // See here for localhost:80 or localhost:9780 ohh also for hamdun soft execution will enter here . But for less than 2? will hamdunsoft.com enter here?
{
tenant = "This is the main domain";
Constant.DatabaseName = "TEST";
if (!values.ContainsKey("tenant"))
values.Add("tenant", tenant);
//return false;
// return true;
}
else if (GetAddress.Length == 2) // execution will enter here for hamdunsoft.com enter here but not for www.hamdunsoft.com
{
tenant = "This is the main domain";
Constant.DatabaseName = GetAddress[0];
if (!values.ContainsKey("tenant"))
values.Add("tenant", tenant);
//return false;
// return true;
}
else if (!values.ContainsKey("tenant")) // for tenant1.hamdunsoft.com execution will enter here
{
values.Add("tenant", tenant);
Constant.DatabaseName = GetAddress[1]+"."+ tenant;
}
return true;
}
}
Upvotes: 0
Reputation: 1347
Stack Exchange itself runs on a multi-tenant architecture!
http://weblogs.asp.net/zowens/archive/2010/06/16/multi-tenant-asp-net-mvc-views.aspx
This set of articles should get you what you need to set up your basic architecture. It has plenty of code and does a pretty good job at covering what you'd need. I don't think you're going to get the exact code that you need here to set up the entire core architecture of your multi tenan application, I'd advise using either this article above or ones like it.
Upvotes: 2