Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to load the files from cdn into a bundle in .net 6.0 #271

Open
KunalSharma1999 opened this issue Mar 9, 2023 · 0 comments
Open

Comments

@KunalSharma1999
Copy link

KunalSharma1999 commented Mar 9, 2023

In ASP.Net MVC following was the way to load files from a cdn link and make it into a bundle:

public static void RegisterBundles(BundleCollection bundles)
{
     bundles.UseCdn = true;
     var version = System.Reflection.Assembly.GetAssembly(typeof(Controllers.HomeController)).GetName().Version.ToString();

    var cdnUrl = ConfigurationManager.AppSettings["cdnUrl"].ToString()+"/{0}?v=" + version;

    bundles.Add(new ScriptBundle("~/bundles/jquery", string.Format(cdnUrl, "bundles/jquery")).Include(
                "~/Scripts/jquery-{version}.js"));
    bundles.Add(new ScriptBundle("~/bundles/jqueryval", string.Format(cdnUrl, "bundles/jqueryval")).Include(
                "~/Scripts/jquery.validate*"));
    bundles.Add(new ScriptBundle("~/bundles/modernizr", string.Format(cdnUrl, "bundles/modernizr")).Include(
                  "~/Scripts/modernizr-*"));
    bundles.Add(new ScriptBundle("~/bundles/bootstrap", string.Format(cdnUrl, "bundles/bootstrap")).Include(
                "~/Scripts/bootstrap.js"));
 }

How can I achieve the same functionality using LigerShark.WebOptimizer in ASP.Net Core MVC project targeting .net 6.0?

I have already added the below mentioned configuration in my appsetting.json file and registered the tag helpers as mentioned in the documentation which states that it will prepend the cdn url but its not working:

"webOptimizer": {
"enableCaching": true,
"enableMemoryCache": true,
"enableDiskCache": true,
"cacheDirectory": "/var/temp/weboptimizercache",
"enableTagHelperBundling": true,
"cdnUrl": "https://mycdnurl.azureedge.net",
 "allowEmptyBundle": false
}

Here is the actual code I have used in my Program.cs file:

builder.Services.AddWebOptimizer(pipeline =>
{
    pipeline.AddJavaScriptBundle("/bundles/jquery.js", "Scripts/jquery-3.3.1.js");
    pipeline.AddJavaScriptBundle("/bundles/jqueryval.js", "Scripts/jquery.validate*");
    pipeline.AddJavaScriptBundle("/bundles/jqueryajax.js", "Scripts/jquery.unobtrusiveajax.min.js");
    pipeline.AddJavaScriptBundle("/bundles/modernizr.js", "Scripts/modernizr-*");
    pipeline.AddJavaScriptBundle("/bundles/bootstrap.js", "Scripts/bootstrap.js");
if (!isDevelopment)
{
     pipeline.MinifyCssFiles();
     pipeline.MinifyJsFiles();
}
},
option =>
{
    option.EnableCaching = true;
    option.EnableDiskCache = false;
    option.EnableMemoryCache = true;
    option.AllowEmptyBundle = false;
    option.EnableTagHelperBundling = true;
    option.CdnUrl = builder.Configuration.GetSection("webOptimizer")["cdnUrl"];
}
);

I also have multiple css files to be bundled and minified but I haven't added that to my code for better clarity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant