Bundle and minify static assets in asp.net mvc core 6

mehmood tekfirst 771 Reputation points
2022-05-30T11:16:51.387+00:00

Hi,

I am using Asp.Net MVC Core 6 Razor View with Jquery.

I need to do Bundle and minify static assets.

I know there is an article for it

https://learn.microsoft.com/en-us/aspnet/core/client-side/bundling-and-minification?view=aspnetcore-6.0

but I want a detail implementation or walk-through on it.

please guide and assist

thank you

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,190 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 56,846 Reputation points
    2022-05-30T18:38:28.837+00:00

    your first step is to pick a minify and bundling tool. you have several options. once you pick one, google for examples

    webpack (what I use and probably most common tool used). along with bundle and optimization supports dynamic loading, transformations.

    https://webpack.js.org

    glup. a dedicated build tool commonly used for javascript and css.

    https://gulpjs.com

    asp.net core middleware (used by some templates and many asp.net developers):

    https://github.com/ligershark/WebOptimizer

    note: you may also want less or sass support for your css files. all the above have plug ins for this support.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. SurferOnWww 1,911 Reputation points
    2022-05-31T03:29:16.597+00:00

    Although the current Microsoft document "Bundle and minify static assets in ASP.NET Core" does not include the detailed implementation, the previous one (sometime around February 2020) describes the implementation using the BuildBundlerMinifier as follows. It is still available for .NET 6.0.

    (1) Add bundleconfig.json right under the project root to specify the files to bundle and minify. Example of bundleconfig.json for wwwroot/css/site.css and wwwroot/js/site.js is as follows:

    [  
      {  
        "outputFileName": "wwwroot/css/site.min.css",  
        "inputFiles": [  
          "wwwroot/css/site.css"  
        ]  
      },  
      {  
        "outputFileName": "wwwroot/js/site.min.js",  
        "inputFiles": [  
          "wwwroot/js/site.js"  
        ],  
        "minify": {  
          "enabled": true,  
          "renameLocals": true  
        },  
        "sourceMap": false  
      }  
    ]  
    

    (2) Install the NuGet package BuildBundlerMinifier.

    206932-nuget.jpg

    (3) Rebuild the project.

    206895-rebuild.jpg

    (4) Confirm the site.min.css and site.min.js are generated.

    206846-minify.jpg

    (5) Modify _Layout.cshtml to use the site.min.css and site.min.js:

    <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" />  
    <script src="~/js/site.min.js" asp-append-version="true"></script>  
    
    2 people found this answer helpful.