Skip to main content

Bundling And Minification Using In ASP.NET Core.

Nowadays no one like to use website which works slowly. Bundling And Minification help us to achive.

Bundling and minification are two different performance optimizer you can use for your application to improve overall performance of application.
Bundling and minification primarily improve the first page request load time. Once a web page has been requested, the browser caches the static assets (JavaScript, CSS, and images).

Bundling :

Bundling combines multiple files into a single file. Bundling reduces the number of server requests which are necessary to render a web asset, such as a web page. You can create any number of individual bundles specifically for CSS, JavaScript, etc. It ultimately reduces http request for static content.

Minification :

Minification removes unnecessary characters from code without altering functionality and also removing white space and commented code. The result is a significant size reduction in requested assets (such as CSS, images, and JavaScript files).

This article we will learn how to implement bundling and minification using BundlerMinifier. thus there are many ways to implement bundling and minification in Asp.Net Mvc Core.

Install BundlerMinifier using nuget manager.

PM> Install-Package BundlerMinifier.Core -Version 2.8.391

Once you install BundlerMinifier we need to update project.csproj file with below code.




Let us understand above code step by step.

Below code is used to define Package Reference used for bundling and minification in our application.



Below Code is used to build minified js and that we need to used as reference.


The dotnet bundle command is registered with the .NET Core CLI only when a <DotNetCliToolReference /> node is used.
This Command="dotnet bundle" will execute at time of compilation and it will build minified and bundled Js referencing bundleconfig.json file.

Configure bundling and minification :

The Asp.Net MVC Core project templates provide a bundleconfig.json configuration file which defines the options for each bundle.
    "outputFileName": "wwwroot/css/default.min.css",
    // An array of relative input file paths. Globbing patterns supported
    "inputFiles": [
    "outputFileName": "wwwroot/js/default.min.js",
    "inputFiles": [
    // Optionally specify minification options
    "minify": {
      "enabled": true,
      "renameLocals": true
    // Optionally generate .map file
    "sourceMap": false

outputFileName: The name of the bundle file to output. Can contain a relative path from the bundleconfig.json file.

inputFiles: An array of files to bundle together. These are relative paths to the configuration file.

minify: The minification options for the output type. optional, default is minify: { enabled: true }

renameLocals : this is used if you want to minified locally declared variable or you want to keep as it is. it cannot rename variable written in string format "Data" if you write in string formate and try to minify it then you minification will not happen thorw error.

sourceMap: Flag indicating whether to generate a source map for the bundled file

refer above image you can see default.min.js has been generated. we can include above generated js file on any razor page. we can also add static file depended upon environment, it can be also used for multiple environment like Development, Production etc. for more detail related to multiple environment support Click Here


I hope this will be helpful for you :)


Popular posts from this blog

Custom AuthorizeAttribute in ASP.NET Core.

Introduction. Filter in MVC are attribute which can be apply to controller and action method. filters allow us to write our custom code before and after method execution. filters can be implemented into different level of scope Global, Class, and Method level. MVC provides different types of filters. The Authorization filter will run first and are used to determine whether the current user is authorized or unauthorized for the current request. In this article we will learn how to create custom authorization filter, were you can write your own authorization framework. Description. For creating Authorization filter IAuthorizationFilter interface must be inherited. For example, the following code demonstrate custom Authorization below CustomAuthorization attribute allow only Home controller to access the particular method under home controller class else it will return "error" in response if it is called via ajaxcall, otherwise it will be redirect to Sessi

How to use Log4Net in .Net Core 2.0 Application.

1. Introduction Logging frameworks are important because they make it easy to write your logs to different places by simply changing your configuration. You can write your .NET logs to a file on disk, a database, a log management system 2. Install Nuget Package For nlog  To use Nlog logging you need to first add Nlog pluging for adding pluging you can do it by different way. 1) Manage Nuget Packages. 2) Nuget command. you can find nuget command for Log4Net below. PM> Install-Package log4net -Version 2.0.8 3. Add log4net.config file We need to Add New to your project with name log4net.config Refer below code for log4net.config for logging log into file. Root is neccesary in log4net.config, in which we can define log level and appender-ref in which we can define appender for eg. FileAppender, ConsoleAppender. Layout : In layout we can define custom parameter sh

What are PascalCase and CamelCase In Programing Language?

What are PascalCase and CamelCase In Programing Language?. Pascal Case  :-  In programming,  PascalCase   means that the first letter of every word in the name is capitalized. In Pascal Case if acronym exists in the word then only the first letter of that acronym should be capital. Eg:  ThisWordIsInPascalCase  . No other characters are used to separate the words, like hyphens or underscores. For example: BackColor TimeUtc FirstName CamelCase  :-    camelCase  or  CamelCase ; also known as  camel caps  or more formally as  medial capitals.  It is the same as Pascal Case, but the first letter of the first word is lowercased. this article calls the two alternatives  upper camel case  (initial upper case letter also known as Title Case) and  lower camel case  (initial lower case letter). Some people and organizations, notably  Microsoft  ,  use the term  camel case  only for lower camel case. For example: backColor timeUtc firstName Snake_case  :-PascalCase and camelCase