How to make unminify packages and list individual files when using a static content server without cookies?

I have two VS projects, one for the main site and one for the “static content” website, where all css, js, images and other static content will be saved and accessible via a cookieless.

So, I have BundleConfig.cs on a static site that creates all the packages:

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)); } } 

And on the main site, I have another BundleConfig.cs, where I point the main site to the static content site as follows:

 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); } } 

Now I can use @Styles.Render("~/bundles/styles") and @Scripts.Render("~/bundles/scripts") , which render as I want, and it works fine:

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

The problem is that the content is always minimized and linked regardless of whether debug=true exists or not. Even if I use BundleTable.EnableOptimization = false in both files, BundleConfig.cs, @Styles.Render() and @Scripts.Render() still display only one tag and link to content that has been reduced.

I understand that the main site will not know the individual files that were included in the static content site, but I hope there is some way to manually specify these paths on the BundleConfig main site so that the Render () methods can list them individually. when optimization is turned off ... if I can make them turn off, that is.

+2
source share
2 answers

Thus, I was able to get this working by adding a custom VirtualPathProvider that does the main project search within the project of static content for individual files. In DEBUG mode, files are listed separately. When in RELEASE mode, mini-nodes are referenced instead of links.

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

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(); } } } } 
+3
source

BundleTable.EnableOptimization = "false" is not an event that can be played here, since your main site refers to a "package" that is always bundled and minimized regardless of debugging status or EnableOptimization.

So @Styles.Render("~/bundles/styles") on a static site will display individual files when BundleTable.EnableOptimization = "false" , but if you go directly to /bundles/styles , you will still get a minipack (which you do from your main site).

One option (perhaps only yours) is to configure the binder on a static site so as not to minimize the package when BundleTable.EnableOptimization = "false" . You can do this by writing a class that inherits from Bundle and uses its own custom IBundleBuilder (you can even write the file name as a comment when you add each file to the bundle). Some sample code pointing you in the right direction can be found on GitHub .

+1
source

Source: https://habr.com/ru/post/1243149/


All Articles