HTML Minification middleware for ASP.NET Core
C#
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
Test/HtmlMinificationMiddleware.Tests
src/HtmlMinificationMiddleware
.gitignore
LICENSE
README.md
appveyor.yml
global.json

README.md

HTML Minification middleware for ASP.NET Core

Minification refers to the process of removing unnecessary or redundant data without affecting how the resource is processed by the browser - e.g. code comments and formatting, removing unused code, using shorter variable and function names, and so on. This repository contains source code of an ASP.NET Core middleware which helps to minify HTML.

How to use HTML Minification middleware for ASP.NET Core

  • Include HtmlMinification Middleware middleware in the project.json file.
{
    "version": "1.0.0-*",
    "webroot": "wwwroot",
    "dependencies": {
        "HtmlMinificationMiddleware": "1.0.0"
    }
}
  • Modify the startup.cs - configure to enable HTML minification.
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
    loggerFactory.AddConsole();
    app.UseIISPlatformHandler();
    app.UseHTMLMinification();
    app.UseDeveloperExceptionPage();
    app.UseMvcWithDefaultRoute();
}
  • Done. Now you can browse the URL.

Excluding URLs from minification.

You can exclude certain pages from minification by using the exclude filter option.

app.UseHTMLMinification("(w*)Page*");

For ASP.NET MVC Core 1.1+

In ASP.NET Core MVC 1.1 onwards, you can use middlewares as Filters, so instead of using the Minification options, you can use MiddlewareFilter attribute, either in Controller or in specific action methods.

[MiddlewareFilter(typeof(HtmlMinificationPipeline))]
public class HomeController : Controller
{
}

or

public class HomeController : Controller
{
    [MiddlewareFilter(typeof(HtmlMinificationPipeline))]
    public IActionResult Index()
    {
        return View();
    }
}

Appveyor Build Status : Build status

Coverage Status : Coverage Status