Vote count:
0
I'm trying to do a multi-tenanted app, where tenants can get their own subdomains e.g.
- tenant1.mysite.com
- tenant2.mysite.com
I've tried using custom routedata, it works only on the first page, somehow on the other pages like /login, /register, etc there's always errors being thrown and it gets very cryptic.
I gave up and went ahead with using a wildcard DNS and let my HomeController determine how to render the View based on the subdomain
ActionFilter
public class SubdomainTenancy : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string subdomain = filterContext.RequestContext.HttpContext.Request.Params["subdomain"]; // A subdomain specified as a query parameter takes precedence over the hostname.
if (subdomain == null)
{
string host = filterContext.RequestContext.HttpContext.Request.Headers["Host"];
int index = host.IndexOf('.');
if (index >= 0)
subdomain = host.Substring(0, index);
filterContext.Controller.ViewBag.Subdomain = subdomain;
}
base.OnActionExecuting(filterContext);
}
}
Home Controller
[SubdomainTenancy]
[AllowAnonymous]
public class HomeController : BaseController
{
public ActionResult Index()
{
string subdomain = ViewBag.Subdomain;
if (subdomain != "www" && !string.IsNullOrEmpty(subdomain) )
{
var db = new tenantdb();
var store = db.GetBySubdomain(subdomain);
if (store == null)
{
return Redirect(HttpContext.Request.Url.AbsoluteUri.Replace(subdomain, "www"));
}
else //it's a valid tenant, let's see if there's a custom layout
{
//load custom view, (if any?)
}
}
return View();
}
}
So now the problem comes when i try to use VirtualPathProvider to load View from database based on the subdomain, but i'm unable to access HttpContext, perhaps due to the life cycle? Now i'm stuck, i've also tried to use RazorEngine to load custom views (from database)
What should i do to support multi-tenancy on my web app that will first search the database for custom views and render using the view in database, else if there's none, fall back to the default /Home/Index.cshtml?
Aucun commentaire:
Enregistrer un commentaire