asp.net – 如何在使用无cookie的静态内容服务器时使bundle无法管理并列出单个文件?

前端之家收集整理的这篇文章主要介绍了asp.net – 如何在使用无cookie的静态内容服务器时使bundle无法管理并列出单个文件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个VS项目,一个用于主网站,一个用于“静态内容”网站,其中所有css,js,图像和其他静态内容将通过无cookie域存储和访问.

所以我的静态站点中有一个BundleConfig.cs,可以创建所有的bundle:

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,我将主站点指向静态内容站点,如下所示:

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>",versionNumber);

    }
}

现在我可以使用@ Styles.Render(“〜/ bundles / styles”)和@ Scripts.Render(“〜/ bundles / scripts”)这样渲染,就像我想要的那样,并且效果很好:

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

我遇到的问题是无论debug = true与否,内容总是被缩小和捆绑.即使我在BundleConfig.cs文件中使用BundleTable.EnableOptimization = false,@ Styles.Render()和@ Scripts.Render()仍然只渲染一个标记并引用缩小的内容.

我知道主站点不知道静态内容站点中捆绑的单个文件,但我希望有一些方法可以在主站点BundleConfig中手动指定这些路径,以便Render()方法可以当优化关闭时单独列出它们……如果我能让它们关闭,那就是.

解决方法

所以我能够通过添加一个自定义的VirtualPathProvider来实现这个功能,它可以在单个文件的静态内容项目中进行主项目搜索.在DEBUG模式下,文件将单独列出.在RELEASE模式下,将引用缩小的包.
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'/>",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",true));
#endif
    }
}

这是我的自定义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(); } }
    }
}
原文链接:https://www.f2er.com/aspnet/249103.html

猜你在找的asp.Net相关文章