我正在尝试 ASP.NET Core Web Optimizer,它在底层使用 NUglify。它非常适合
js
和 css
,并且显然也支持 html
,但我无法让它与 razor cshtml 一起使用。添加 services.AddWebOptimizer(p => p.MinifyHtmlFiles());
似乎没有任何作用,将 app.UseWebOptimizer();
移动到 app.UseMvc();
之后也不起作用。
有什么想法吗?
使用名为 WebMarkupMin.AspNetCore5 的库会有帮助
nuget 包链接 - https://www.nuget.org/packages/WebMarkupMin.AspNetCore5/
参考-https://stackoverflow.com/a/65879818/9641914
注意:提到的库适用于.net 5
using WebMarkupMin.AspNet.Common.Compressors;
using WebMarkupMin.AspNetCore8;
using WebMarkupMin.Core;
using WebMarkupMin.NUglify;
builder.Services.AddWebMarkupMin(
options =>
{
options.AllowMinificationInDevelopmentEnvironment = true;
options.AllowCompressionInDevelopmentEnvironment = true;
})
.AddHtmlMinification(options =>
{
HtmlMinificationSettings settings = options.MinificationSettings;
settings.RemoveRedundantAttributes = true;
settings.RemoveHttpProtocolFromAttributes = true;
options.CssMinifierFactory = new NUglifyCssMinifierFactory();
options.JsMinifierFactory = new NUglifyJsMinifierFactory();
})
.AddHttpCompression(options =>
{
options.CompressorFactories =
[
new BuiltInBrotliCompressorFactory(new BuiltInBrotliCompressionSettings
{
Level = CompressionLevel.Fastest
}),
new DeflateCompressorFactory(new DeflateCompressionSettings
{
Level = CompressionLevel.Fastest
}),
new GZipCompressorFactory(new GZipCompressionSettings
{
Level = CompressionLevel.Fastest
})
];
});
app.UseWebMarkupMin();
干得好...