ASP.NET捆绑和缩小并从其他Web应用程序使用它

Mr.*_*kin 2 javascript asp.net bundle minify

我有ASP.NET MVC 4应用程序,其中包含一些简单的jQuery小部件库.现在我想允许其他Web应用程序使用该库.不是在客户端页面上按文件插入每个窗口小部件文件,而是在一个请求中将它们全部作为捆绑加载.有人知道是否可以使用Microsoft ASP.NET Web Optimization Framework执行该捆绑?换句话说,我想准备一些"jquery-library-1.0.0.js"文件并允许其他应用程序加载它.

我所能找到的是如何在MVC应用程序中使用它,而没有关于如何使用静态名称准备bundle.

Mr.*_*kin 6

经过一些研究后,我找到了如何做到这一点 - 使用IBundleTransform接口.它允许访问捆绑包内容,我只需将其转储到我想要的地方的磁盘上,稍后将其用于任何其他想要使用该库的应用程序.

public class ScriptsBundleTransform : IBundleTransform
{
    public string ScriptsPath { get; set; }
    public string Version { get; set; }
    public string Minified { get; set; }
    public string Full { get; set; }

    public ScriptsBundleTransform()
    {
    }

    public ScriptsBundleTransform(string path, string version, string minified, string full)
    {
        ScriptsPath = path;
        Version = version;
        Minified = minified;
        Full = full;
    }

    public void Process(BundleContext context, BundleResponse response)
    {
        string scriptsRoot = context.HttpContext.Server.MapPath(Path.Combine(ScriptsPath, Version));

        if (!Directory.Exists(scriptsRoot))
            Directory.CreateDirectory(scriptsRoot);

        //  if minified file name specified...
        if (!string.IsNullOrEmpty(Minified))
        {
            using (TextWriter writer = File.CreateText(Path.Combine(scriptsRoot, Minified)))
            {
                writer.Write(response.Content);
            }
        }

        //  if full file name specified...
        if (!string.IsNullOrEmpty(Full))
        {
            using (Stream writer = File.OpenWrite(Path.Combine(scriptsRoot, Full)))
            {
                foreach (var file in response.Files)
                {
                    file.VirtualFile.Open().CopyTo(writer);
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

之后我只需要将bundle config中的这个转换器添加到我想要转储到磁盘的bundle中:

            widgets.Transforms.Add(new ScriptsBundleTransform()
            {
                Version = "1.0.0",
                ScriptsPath = "~/Scripts",
                Minified = "jquery.library.min.js",
                Full = "jquery.library.js"
            });
Run Code Online (Sandbox Code Playgroud)

即使库中的任何窗口小部件将被更改,转储文件也将自动重新生成,我没有手动控制此过程.