ASP.NET Core Latest - Taritsyn/WebMarkupMin GitHub Wiki
WebMarkupMin.AspNetCoreLatest package is suitable for use in web applications written in ASP.NET Core 8, 9 and 10.
This package contains one ASP.NET Core Middleware - WebMarkupMinMiddleware.
In ASP.NET Core 8+ applications configuring of WebMarkupMin maked in Program.cs:
using WebMarkupMin.AspNetCoreLatest;
…
var builder = WebApplication.CreateBuilder(args);
#region Configure services
IServiceCollection services = builder.Services;
…
// Add WebMarkupMin services.
services.AddWebMarkupMin()
.AddHtmlMinification()
.AddXmlMinification()
.AddHttpCompression()
;
// Add framework services.
services.AddControllersWithViews();
…
#endregion
#region Configure the HTTP request pipeline
var app = builder.Build();
…
app.UseWebMarkupMin();
app.MapControllerRoute(
…
);
#endregion
app.Run();In the Configure services region of Program.cs file 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:
-
AddWebMarkupMinmethod adds a following services in the form of singletons:NullLogger(implementation ofILoggerinterface),KristensenCssMinifierFactory(implementation ofICssMinifierFactoryinterface) andCrockfordJsMinifierFactory(implementation ofIJsMinifierFactoryinterface). -
AddHtmlMinificationmethod adds aHtmlMinificationManager(implementation ofIHtmlMinificationManagerinterface) service in the form of singleton. Also there is a similar method –AddXhtmlMinification, which registersXhtmlMinificationManagerclass as an implementation ofIXhtmlMinificationManagerinterface. -
AddXmlMinificationmethod adds aXmlMinificationManager(implementation ofIXmlMinificationManagerinterface) service in the form of singleton. -
AddHttpCompressionmethod adds aHttpCompressionManager(implementation ofIHttpCompressionManagerinterface) 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 the HTTP request pipeline region of Program.cs file 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 MapControllerRoute 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.IO.Compression;
using WebMarkupMin.AspNet.Common.Compressors;
using WebMarkupMin.AspNet.Common.UrlMatchers;
using WebMarkupMin.AspNetCoreLatest;
using WebMarkupMin.Core;
using WebMarkupMin.NUglify;
…
#region Configure 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
})
};
})
;
…
#endregion
…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 WebMarkupMin.AspNetCoreLatest;
using WebMarkupMin.Core;
using WebMarkupMin.NUglify;
…
// 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 WebMarkupMin.AspNetCoreLatest;
…
using IWmmLogger = WebMarkupMin.Core.Loggers.ILogger;
using WmmAspNetCoreLogger = WebMarkupMin.AspNetCoreLatest.AspNetCoreLogger;
…
// Override the default logger for WebMarkupMin.
services.AddSingleton<IWmmLogger, WmmAspNetCoreLogger>();
…
// Add WebMarkupMin services to the services container.
services.AddWebMarkupMin(…)
…