ASP.NET Core 3.1+ - Taritsyn/WebMarkupMin GitHub Wiki

WebMarkupMin.AspNetCore3 package is suitable for use in web applications written in ASP.NET Core 3.1 and 5.

This package contains one ASP.NET Core Middleware - WebMarkupMinMiddleware.

Specificity of configuration

In ASP.NET Core 3.1+ applications configuring of WebMarkupMin maked in Startup.cs:

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using WebMarkupMin.AspNetCore3;namespace WebMarkupMin.Sample.AspNetCore31.Mvc31
{
    public class Startup
    {// This method gets called by the runtime.
        // Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {// Add WebMarkupMin services.
            services.AddWebMarkupMin()
                .AddHtmlMinification()
                .AddXmlMinification()
                .AddHttpCompression()
                ;

            // Add framework services.
            services.AddControllersWithViews();}

        // This method gets called by the runtime.
        // Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {app.UseWebMarkupMin();

            app.UseEndpoints(endpoints =>
            {});
        }
    }
}

In the ConfigureServices method of Startup class is performed setup of the dependency injection. Using the AddWebMarkupMin extension method and its child methods (AddHtmlMinification, AddXmlMinification and AddHttpCompression) we add WebMarkupMin services to the services container:

  1. AddWebMarkupMin method adds a following services in the form of singletons: NullLogger (implementation of ILogger interface), KristensenCssMinifierFactory (implementation of ICssMinifierFactory interface) and CrockfordJsMinifierFactory (implementation of IJsMinifierFactory interface).
  2. AddHtmlMinification method adds a HtmlMinificationManager (implementation of IHtmlMinificationManager interface) service in the form of singleton. Also there is a similar method – AddXhtmlMinification, which registers XhtmlMinificationManager class as an implementation of IXhtmlMinificationManager interface.
  3. AddXmlMinification method adds a XmlMinificationManager (implementation of IXmlMinificationManager interface) service in the form of singleton.
  4. AddHttpCompression method adds a HttpCompressionManager (implementation of IHttpCompressionManager interface) service in the form of singleton.

In fact, child methods are responsible for plugging of individual WebMarkupMin features (for example, if you do not call AddHtmlMinification method, then HTML minification will not be available).

In the Configure method of Startup class is performed registration of middlewares. Using the UseWebMarkupMin method we add an instance of WebMarkupMinMiddleware class to the ASP.NET request pipeline. Calling of this method must be done directly before calling of UseEndpoints method or another method responsible for registering routes, because RouterMiddleware completes call chain of middlewares.

Consider a example of advanced configuring of the WebMarkupMin services:

using System.Collections.Generic;
using System.IO.Compression;

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

using WebMarkupMin.AspNet.Common.Compressors;
using WebMarkupMin.AspNet.Common.UrlMatchers;
using WebMarkupMin.AspNetCore3;
using WebMarkupMin.Core;
using WebMarkupMin.NUglify;namespace WebMarkupMin.Sample.AspNetCore31.Mvc31
{
    public class Startup
    {// This method gets called by the runtime.
        // Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {// Add WebMarkupMin services.
            services.AddWebMarkupMin(options =>
            {
                options.AllowMinificationInDevelopmentEnvironment = true;
                options.AllowCompressionInDevelopmentEnvironment = true;
            })
                .AddHtmlMinification(options =>
                {
                    options.ExcludedPages = new List<IUrlMatcher>
                    {
                        new WildcardUrlMatcher("/minifiers/x*ml-minifier"),
                        new ExactUrlMatcher("/contact")
                    };

                    HtmlMinificationSettings settings = options.MinificationSettings;
                    settings.RemoveRedundantAttributes = true;
                    settings.RemoveHttpProtocolFromAttributes = true;
                    settings.RemoveHttpsProtocolFromAttributes = true;

                    options.CssMinifierFactory = new NUglifyCssMinifierFactory();
                    options.JsMinifierFactory = new NUglifyJsMinifierFactory();
                })
                .AddXhtmlMinification(options =>
                {
                    options.IncludedPages = new List<IUrlMatcher>
                    {
                        new WildcardUrlMatcher("/minifiers/x*ml-minifier"),
                        new ExactUrlMatcher("/contact")
                    };

                    XhtmlMinificationSettings settings = options.MinificationSettings;
                    settings.RemoveRedundantAttributes = true;
                    settings.RemoveHttpProtocolFromAttributes = true;
                    settings.RemoveHttpsProtocolFromAttributes = true;

                    options.CssMinifierFactory = new KristensenCssMinifierFactory();
                    options.JsMinifierFactory = new CrockfordJsMinifierFactory();
                })
                .AddXmlMinification(options =>
                {
                    XmlMinificationSettings settings = options.MinificationSettings;
                    settings.CollapseTagsWithoutContent = true;
                })
                .AddHttpCompression(options =>
                {
                    options.CompressorFactories = new List<ICompressorFactory>
                    {
                        new BuiltInBrotliCompressorFactory(new BuiltInBrotliCompressionSettings
                        {
                            Level = CompressionLevel.Fastest
                        }),
                        new DeflateCompressorFactory(new DeflateCompressionSettings
                        {
                            Level = CompressionLevel.Fastest
                        }),
                        new GZipCompressorFactory(new GZipCompressionSettings
                        {
                            Level = CompressionLevel.Fastest
                        })
                    };
                })
                ;}}
}

From the above code it is seen, that the WebMarkupMin methods (AddWebMarkupMin, AddHtmlMinification, AddXhtmlMinification, AddXmlMinification and AddHttpCompression) now take delegates as parameters, through which you can specify the markup minification options (WebMarkupMinOptions, HtmlMinificationOptions, XhtmlMinificationOptions, XmlMinificationOptions and HttpCompressionOptions).

If the values of the CssMinifierFactory and JsMinifierFactory properties are not explicitly specified in the HTML/XHTML minification options, then they will be obtained from the services container. To override their default values, you need to register new implementations of the ICssMinifierFactory and IJsMinifierFactory interfaces as services:

using Microsoft.Extensions.DependencyInjection;
…

…
using WebMarkupMin.AspNetCore3;
using WebMarkupMin.Core;
using WebMarkupMin.NUglify;public void ConfigureServices(IServiceCollection services)
        {// Override the default CSS and JS minifier factories for WebMarkupMin.
            services.AddSingleton<ICssMinifierFactory, NUglifyCssMinifierFactory>();
            services.AddSingleton<IJsMinifierFactory, NUglifyJsMinifierFactory>();// Add WebMarkupMin services to the services container.
            services.AddWebMarkupMin()}

Similarly, you can change the logger, that is used by default:

using Microsoft.Extensions.DependencyInjection;
…

…
using WebMarkupMin.AspNetCore3;
using WebMarkupMin.Core;using IWmmLogger = WebMarkupMin.Core.Loggers.ILogger;
using WmmAspNetCoreLogger = WebMarkupMin.AspNetCore3.AspNetCoreLogger;public void ConfigureServices(IServiceCollection services)
        {// Override the default logger for WebMarkupMin.
            services.AddSingleton<IWmmLogger, WmmAspNetCoreLogger>();// Add WebMarkupMin services to the services container.
            services.AddWebMarkupMin()}
⚠️ **GitHub.com Fallback** ⚠️