Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti Tenant application using MapRoute
    text
    copied!<p>I have found a solution for implement multi-tenant in my asp.net mvc project and I want know if it's correct or exist a better way.</p> <p>I want organize more customers using the same application handling the web request, for example:</p> <pre><code>http://mysite/&lt;customer&gt;/home/index //home is controller and index the action </code></pre> <p>For this reason i changed the default maproute:</p> <pre><code>routes.MapRoute( name: "Default", url: "{customername}/{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); </code></pre> <p>and I implemented a custom ActionFilterAttribute:</p> <pre><code>public class CheckCustomerNameFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting( ActionExecutingContext filterContext ) { var customerName = filterContext.RouteData.Values["customername"]; var customerRepository = new CustomerRepository(); var customer = customerRepository.GetByName( customerName ); if( customer == null ) { filterContext.Result = new ViewResult { ViewName = "Error" }; } base.OnActionExecuting( filterContext ); } } </code></pre> <p>and using it:</p> <pre><code>public class HomeController : Controller { [CheckCustomerNameFilterAttribute] public ActionResult Index() { var customerName = RouteData.Values["customername"]; // show home page of customer with name == customerName return View(); } } </code></pre> <p>With this solution i can switch customer using customer name and correctly accept requests like this:</p> <pre><code>http://mysite/customer1 http://mysite/customer2/product/detail/2 ................................... </code></pre> <p>This solution works well but I don't know if the best approach. Does anyone know a better way?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload