如何使束unminify并列出具体文件使用Cookie的静态内容服务器时? [英] How to make bundles unminify and list individual files when using a cookieless static content server?

本文介绍了如何使束unminify并列出具体文件使用Cookie的静态内容服务器时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个VS项目,一个用于主网站,另一个是静态内容的网站,所有的CSS,JS,图片等静态内容将通过Cookie的域存储和访问。

I have two VS projects, one for the main website and one for a "static content" website where all the css, js, images, and other static content will be stored and accessed via a cookieless domain.

所以,我在我的静态网站BundleConfig.cs创建所有的软件包:

So I have a BundleConfig.cs in my static site that creates all the bundles:

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new StyleBundle("~/bundles/styles").IncludeDirectory("~/app/styles", "*.css", true));
        bundles.Add(new ScriptBundle("~/bundles/scripts").IncludeDirectory("~/app/src", "*.js", true));
    }
}

和主网站我有另一个BundleConfig.cs,我点的主要场所,以静态内容的网站是这样的:

And in the main site I have another BundleConfig.cs where I point the main site to the static content site like this:

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        var staticWebsite = ConfigurationManager.AppSettings["StaticWebsite"];
        var versionNumber = ConfigurationManager.AppSettings["VersionNumber"];

        Styles.DefaultTagFormat = string.Format("<link href='{0}{{0}}?v={1}' rel='stylesheet'/>", staticWebsite, versionNumber);
        Scripts.DefaultTagFormat = string.Format("<script src='{0}{{0}}?v={1}'></script>", staticWebsite, versionNumber);

    }
}

现在我可以使用 @ Styles.Render(〜/包/风格) @ Scripts.Render(〜/包/这使得这个样子,现在的样子脚本)我要和它的伟大工程:

Now I can use @Styles.Render("~/bundles/styles") and @Scripts.Render("~/bundles/scripts") which render like this, just the way I want and it works great:

<link href='http://mycookielessdomain.com/bundles/styles?v=1.0.0.0' rel='stylesheet'/>
<script src='http://mycookielessdomain.com/bundles/scripts?v=1.0.0.0'></script>

我的问题是,内容是的总是的精缩且不论是否捆绑调试=真与否。即使我在这两个BundleConfig.cs文件中使用 BundleTable.EnableOptimization = FALSE @ Styles.Render() @ Scripts.Render()仍然只呈现每个一个标签,并指已经过压缩的内容。

The problem I have is that the content is always minified and bundled regardless of whether debug=true or not. Even if I use BundleTable.EnableOptimization = false in both BundleConfig.cs files, @Styles.Render() and @Scripts.Render() still only render one tag each and refer to content that is minified.

据我了解,主要的网站就没有在静态内容网站被捆绑的单个文件的知识,但我希望的是,有一些方法在主站点手动指定这些路径BundleConfig使渲染( )方法可单独列出来优化时关闭,如果我曾经让他们关闭,这是。

I understand that the main site would have no knowledge of the individual files that were bundled in the static content site, but my hope is that there is some way to manually specify these paths in the main site BundleConfig so that the Render() methods can list them individually when optimizations are off... if I can ever get them to turn off, that is.

推荐答案

所以,我能够通过添加自定义的VirtualPathProvider这使得单个文件的静态内容项目中的主项目搜索,获得这个工作。在调试模式下,文件被单独列出。当在释放模式中,精缩束引用来代替。

So I was able to get this working by adding a custom VirtualPathProvider which makes the main project search within the static content project for the individual files. When in DEBUG mode, the files are listed individually. When in RELEASE mode, the minified bundles are referenced instead.

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        var staticWebsite = ConfigurationManager.AppSettings["StaticWebsite"];
        var versionNumber = ConfigurationManager.AppSettings["VersionNumber"];

        Styles.DefaultTagFormat = string.Format("<link href='{0}{{0}}?v={1}' rel='stylesheet'/>", staticWebsite, versionNumber);
        Scripts.DefaultTagFormat = string.Format("<script src='{0}{{0}}?v={1}'></script>", staticWebsite, versionNumber);

#if DEBUG
        // Includes files from the static content project so they can be listed individually if in DEBUG mode.
        BundleTable.VirtualPathProvider = new StaticContentVirtualPathProvider();
        bundles.Add(new StyleBundle("~/bundles/styles").IncludeDirectory("~/app/styles", "*.css", true));
        bundles.Add(new ScriptBundle("~/bundles/scripts").IncludeDirectory("~/app/src", "*.js", true));
#endif
    }
}

下面是我的自定义的VirtualPathProvider:

Here is my custom VirtualPathProvider:

public class StaticContentVirtualPathProvider : VirtualPathProvider
{
    // Modify this to be the relative path from your main project to your static content project.
    private const string StaticContentRelativePath = @"..\..\MyStaticContentProjectFolder";

    public static string GetStaticContentPath(string virtualPath, bool isDirectory = false)
    {
        virtualPath = virtualPath.Replace('/', '\\').Replace("~", "");

        if (isDirectory && !virtualPath.EndsWith("\\")) virtualPath += "\\";

        return HttpRuntime.AppDomainAppPath + StaticContentRelativePath + virtualPath;
    }

    public override bool FileExists(string virtualPath)
    {
        return File.Exists(GetStaticContentPath(virtualPath));
    }

    public override bool DirectoryExists(string virtualDir)
    {
        return Directory.Exists(GetStaticContentPath(virtualDir));
    }

    public override VirtualFile GetFile(string virtualPath)
    {
        return new StaticContentVirtualFile(virtualPath);
    }

    public override VirtualDirectory GetDirectory(string virtualDir)
    {
        return new StaticContentVirtualDirectory(virtualDir);
    }

    private class StaticContentVirtualFile : VirtualFile
    {
        public StaticContentVirtualFile(string virtualPath)
            : base(virtualPath)
        {
            this.virtualPath = virtualPath;
        }

        private readonly string virtualPath;

        public override Stream Open()
        {
            return new FileStream(StaticContentVirtualPathProvider.GetStaticContentPath(virtualPath), FileMode.Open);
        }
    }

    internal class StaticContentVirtualDirectory : VirtualDirectory
    {
        public StaticContentVirtualDirectory(string virtualPath)
            : base(virtualPath)
        {
        }

        public override IEnumerable Files
        {
            get
            {
                var filePaths = Directory.GetFiles(GetStaticContentPath(this.VirtualPath, true));
                var files = filePaths.Select(filePath => new StaticContentVirtualFile(this.VirtualPath + Path.GetFileName(filePath))).ToList();
                return files;
            }
        }

        public override IEnumerable Directories
        {
            get
            {
                var subDirectoryPaths = Directory.GetDirectories(GetStaticContentPath(this.VirtualPath, true));
                var dirs = subDirectoryPaths.Select(subDirectoryPath => new StaticContentVirtualDirectory(this.VirtualPath + Path.GetFileName(subDirectoryPath))).ToList();
                return dirs;
            }
        }

        public override IEnumerable Children { get { throw new NotImplementedException(); } }
    }
}

这篇关于如何使束unminify并列出具体文件使用Cookie的静态内容服务器时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆