【问题标题】:Multi Tenant application using MapRoute使用 MapRoute 的多租户应用程序
【发布时间】:2012-09-26 09:05:05
【问题描述】:

我在我的 asp.net mvc 项目中找到了实现多租户的解决方案,并且 我想知道它是否正确或是否存在更好的方法。

我想组织更多客户使用处理网络请求的同一应用程序,例如:

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
...................................

此解决方案效果很好,但我不知道是否是最佳方法。 有谁知道更好的方法吗?

【问题讨论】:

    标签: asp.net-mvc multi-tenant


    【解决方案1】:

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

    public ActionResult Index(string customerName)
    {
    }
    

    【讨论】:

      猜你喜欢
      • 2018-08-29
      • 2011-05-05
      • 2015-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2014-07-18
      • 1970-01-01
      相关资源
      最近更新 更多