Asp.net mvc 使用MapRoute的多租户应用程序

Asp.net mvc 使用MapRoute的多租户应用程序,asp.net-mvc,multi-tenant,Asp.net Mvc,Multi Tenant,我在我的asp.net mvc项目和 我想知道这是正确的还是存在更好的方法 我希望组织更多的客户使用相同的应用程序处理web请求,例如: http://mysite/<customer>/home/index //home is controller and index the action 我实现了一个定制的ActionFilterAttribute: public class CheckCustomerNameFilterAttribute : ActionFil

我在我的asp.net mvc项目和 我想知道这是正确的还是存在更好的方法

我希望组织更多的客户使用相同的应用程序处理web请求,例如:

http://mysite/<customer>/home/index        //home is controller and index the action
我实现了一个定制的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
...................................
这个解决方案很有效,但我不知道是否是最好的方法。
有人知道更好的方法吗?

您可以对客户名称进行模型绑定,而不必从路由值中提取:

public ActionResult Index(string customerName)
{
}
public ActionResult Index(string customerName)
{
}