Asp.net mvc 4 ASP.NET捆绑和缩小包括来自数据库的动态文件

Asp.net mvc 4 ASP.NET捆绑和缩小包括来自数据库的动态文件,asp.net-mvc-4,multi-tenant,bundling-and-minification,Asp.net Mvc 4,Multi Tenant,Bundling And Minification,我正在开发一个多租户MVC4应用程序,用户可以在该应用程序上进行主题化。 他可以覆盖每一个资源css、js、jpg、png等。。。通过向主题表添加相对路径,例如/Scripts/booking.js 使用哪个租户由URL决定,例如:。http://myapp/tenant/Booking/New 这只是应该使用的连接字符串的名称 因此,如果对特定资源发出请求,我首先需要检查数据库中是否存在此资源的重写版本,如果找到,则使用它 现在,我想实现microsoft在System.Web.Optimiz

我正在开发一个多租户MVC4应用程序,用户可以在该应用程序上进行主题化。 他可以覆盖每一个资源css、js、jpg、png等。。。通过向主题表添加相对路径,例如/Scripts/booking.js

使用哪个租户由URL决定,例如:。http://myapp/tenant/Booking/New 这只是应该使用的连接字符串的名称

因此,如果对特定资源发出请求,我首先需要检查数据库中是否存在此资源的重写版本,如果找到,则使用它

现在,我想实现microsoft在System.Web.Optimization命名空间中提供的新绑定和缩小功能。但是我不知道如何通过数据库中的文件来实现这一点

我已经原型化了自己的JsMinify实现来实现这一点

public class MyJsMinify : JsMinify
{
    private static byte[] GetContentFile(FileInfo filePath)
    {
        string fullName = filePath.FullName;
        int indexOf = fullName.IndexOf("content", StringComparison.OrdinalIgnoreCase);
        string substring = fullName.Substring(indexOf + 8).Replace(@"\\", "/").Replace(@"\", "/");

        ThemingService themingService = ObjectFactory.GetInstance<ThemingService>();
        Theming myTheming = themingService.Find(new ThemingFilter { FilePathLike = substring });
        if (myTheming == null)
        {
            return themingService.GetContentFile(fullName);
        }
        return myTheming.FileData;
    }

    public override void Process(BundleContext context, BundleResponse response)
    {
        StringBuilder newContent = new StringBuilder();
        foreach (FileInfo fileInfo in response.Files)
        {
            using (MemoryStream memoryStream = new MemoryStream(GetContentFile(fileInfo)))
            {
                using (StreamReader myStreamReader = new StreamReader(memoryStream, true))
                {
                    newContent.AppendLine(myStreamReader.ReadToEnd());
                }
            }
        }

        response.Content = newContent.ToString();

        base.Process(context, response);
    }
}
ContentFile方法如下所示

<script src="/myapp/tenant/Content/Scripts/jquery-1.9.0.js"></script>
    [AcceptVerbs(HttpVerbs.Get)]
    [AcceptType(HttpTypes.All)]
    [OutputCache(CacheProfile = "ContentFile")]
    public ActionResult ContentFile(string filePath)
    {
        if (string.Compare(filePath, "Stylesheets/Import.css", StringComparison.OrdinalIgnoreCase) == 0)
        {
            return GetContentImport(CssFileArray, "Stylesheets/");
        }
        if (string.Compare(filePath, "Stylesheets/ImportOutlook.css", StringComparison.OrdinalIgnoreCase) == 0)
        {
            return GetContentImport(OutlookCssFileArray, "Stylesheets/");
        }
        if (string.Compare(filePath, "Scripts/OutlookAddin/Import.js", StringComparison.OrdinalIgnoreCase) == 0)
        {
            return GetContentImport(OutlookJsFileArray, "Scripts/");
        }
        return new FileContentResult(GetContentFile(filePath), MimeType(filePath));
    }
有人知道我怎样才能做到这一点吗?
是否有多租户模式可以遵循?

因此我不确定是否完全理解您的场景,但我相信这就是VirtualPathProviders的用途

我们在1.1-alpha1版本中添加了支持,因此捆绑包将自动使用在ASP.NET中注册的VirtualPathProvider获取文件内容

如果您要编写一个能够始终返回~/Scripts/booking.js正确版本的自定义VPP,那么一切都应该正常工作

routeCollection.MapRoute("Content1", "{mandator}/Content/{*filePath}", new { mandator = defaultMandator, controller = "Environment", action = "ContentFile" }, new { mandator = mandatorConstraints });
routeCollection.MapRoute("Content2", "Content/{*filePath}", new { mandator = defaultMandator, controller = "Environment", action = "ContentFile" }, new { mandator = mandatorConstraints });
    [AcceptVerbs(HttpVerbs.Get)]
    [AcceptType(HttpTypes.All)]
    [OutputCache(CacheProfile = "ContentFile")]
    public ActionResult ContentFile(string filePath)
    {
        if (string.Compare(filePath, "Stylesheets/Import.css", StringComparison.OrdinalIgnoreCase) == 0)
        {
            return GetContentImport(CssFileArray, "Stylesheets/");
        }
        if (string.Compare(filePath, "Stylesheets/ImportOutlook.css", StringComparison.OrdinalIgnoreCase) == 0)
        {
            return GetContentImport(OutlookCssFileArray, "Stylesheets/");
        }
        if (string.Compare(filePath, "Scripts/OutlookAddin/Import.js", StringComparison.OrdinalIgnoreCase) == 0)
        {
            return GetContentImport(OutlookJsFileArray, "Scripts/");
        }
        return new FileContentResult(GetContentFile(filePath), MimeType(filePath));
    }