1
我在我的asp.net mvc項目中找到了實現多租戶的解決方案,並且我想知道它是正確還是存在更好的方法。使用MapRoute的多租戶應用程序
我想用相同的應用程序處理Web請求組織更多的客戶,例如:
http://mysite/<customer>/home/index //home is controller and index the action
出於這個原因,我改變了默認圖路線:
routes.MapRoute(
name: "Default",
url: "{customername}/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
,我實現了自定義ActionFilterAttribute :
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);
}
}
並使用它:
public class HomeController : Controller
{
[CheckCustomerNameFilterAttribute]
public ActionResult Index()
{
var customerName = RouteData.Values["customername"];
// show home page of customer with name == customerName
return View();
}
}
有了這個解決方案,我可以使用客戶名稱切換客戶和正確接受這樣的請求:
http://mysite/customer1
http://mysite/customer2/product/detail/2
...................................
此解決方案很好,但我不知道是不是最好的辦法。 有誰知道更好的方法?