如何处理多租户ASP MVC站点的路径设置

时间:2016-03-22 10:25:32

标签: c# css asp.net-mvc asp.net-mvc-4

我正在开发一个需要支持基于主机的多租户的网站,而且我已经完成了整个部分。我遇到的问题是我在CSS文件夹中有每个租户的子文件夹(1,2,3)。

CSS
    |_ tenant_1
    |_ tenant_2
    |_ tenant_3
    |_ tenant (virtual)
tenant_X 文件夹中的

有自定义css文件,用于对每个特定租户进行stypling。

我的想法是以某种方式创建一个虚拟位置(租户),该位置将映射到租户的文件夹,并且_Layout中只需要一个额外的coude线。我在MVC中并不深刻,到目前为止我知道,我认为我可以使用自定义路径来实现这一点。 这种方法的另一个原因是租户用户不允许看到有其他租户。我必须排除让用户加载错误文件的可能性。

这是正确的做法吗?你能建议更好的方式吗?

1 个答案:

答案 0 :(得分:1)

通过向_Layout页面添加1行来实现此目的的一种可能的实现方法可能是从控制器获取css文件作为text / css。

因此,假设当前的租户ID在前端可用,您可以在具有该ID的控制器上调用方法

例如:

@Styles.Render(string.Format("/CustomizationController/GetCssForTenant?tenantId={0}", loggedTeanant == null ? (int?) null : loggedTenant.Id))

现在使用以下方法创建自定义控制器

public class CustomizationController : Controller
{
    //this will cache cliente side the css file but if the duration expires
    // or the tenantId changes it will be ask for the new file
    [OutputCache(Duration = 43200, VaryByParam = "tenantId")]
    public FileResult GetCssForTenant(int? tenantId)
    {
        var contentType = "text/css";
        //if teanant id is null return empty css file
        if(!tenantID.HasValue)
                return new FileContentResult(new byte[0], contentType);

        //load the real css file here <-
        var result = ...

        //---
        //if having problems with the encoding use this ...
        //System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        //var content = encoding.GetBytes(result);
        //---

        Response.ContentType = contentType;

        return new FileContentResult(result, contentType);
        //return new FileContentResult(content, contentType);
    }
}

希望这有助于实现您的需求。请记住,这是可能实现的草图

修改 如果您想快速尝试我建议的实现,请使用此

public class CustomizationController : Controller
{
    //this will cache cliente side the css file but if the duration expires
    // or the tenantId changes it will be ask for the new file
    [OutputCache(Duration = 43200, VaryByParam = "tenantId")]
    public FileResult GetCssForTenant(int? tenantId)
    {
        var contentType = "text/css";
        //if teanant id is null return empty css file
        if(!tenantID.HasValue)
                return new FileContentResult(new byte[0], contentType);

        //load the real css file here <-
        var result = Environment.NewLine;

        if(tenantID = 1)
            result "body{ background-color: black !important;}"
        else
            result "body{ background-color: pink !important;}"

        result += Environment.NewLine;

        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
        var content = encoding.GetBytes(result);

        Response.ContentType = contentType;

        return new FileContentResult(result, contentType);
    }
}

并更改_Layout

@Styles.Render(string.Format("/CustomizationController/GetCssForTenant?tenantId={0}", 1))

现在,如果发送1,页面的背景颜色应变为黑色,如果发送2,则变为粉红色。 您还可以在网络中看到,如果您使用相同的ID请求2次,则状态将为304,这意味着该文件来自缓存。 如果您更改了ID,则状态将为200,即未缓存的响应。

如果您传递null,则css文件将变为空,因此它将回退到您的默认css。