Last spring, when ASP.NET 5 was still in Beta 3, I started receiving emails from users asking them to make WebMarkupMin compatible with DNX 4.5.1 and DNX Core 5.0. The main problem was that the new .NET did not support customization using the App.config
and Web.config
configuration files. Rewriting WebMarkupMin.Core, WebMarkupMin.MsAjax and WebMarkupMin.Yui shouldn’t have been difficult, because you just had to remove all the code that uses the System.Configuration
library. Serious problems should have occurred when rewriting ASP.NET extensions, because they needed to develop a completely new configuration model, and this, in turn, required very serious changes in the architecture. These changes affected not only the code, but also the solution structure and NuGet packages, so I decided to start from scratch and made a repository on GitHub . At that time, before the release of the stable version of ASP.NET, there remained at least six months, so it was necessary to simultaneously support 2 WebMarkupMin branches: stable 1.X on CodePlex and preliminary 2.X on GitHub.As everyone knows, the release of stable versions of .NET and ASP.NET Core 1.0 was delayed for several more months and took place only at the end of June of this year. Following the release of these frameworks, the release of WebMarkupMin 2.0 took place. In this article I will tell you how to update existing applications under WebMarkupMin 2.X, as well as how to add it to web applications written in ASP.NET Core.Breaking Changes and Innovations
In order to install WebMarkupMin 2.X packages, you need to upgrade the NuGet Package Manager to version 2.8.6 or higher.
')
The main breaking of version 2.X was the rejection of the use of the
Web.config
and
App.config
files to configure WebMarkupMin. Now, when setting up, instead of a declarative approach (use of configuration files), the imperative approach is used (use of program code).
Therefore, when upgrading WebMarkupMin to version 2.X, you need to do 2 things:
- Remove the WebMarkupMin.ConfigurationIntelliSense package, because it is no longer necessary.
- After removing obsolete packages and updating old versions of packages to new ones, you need to remove the
webMarkupMin
configuration section webMarkupMin
and its declaration from the Web.config
and App.config
files:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> … <sectionGroup name="webMarkupMin"> … </sectionGroup> … </configSections> … <webMarkupMin xmlns="http://tempuri.org/WebMarkupMin.Configuration.xsd"> … </webMarkupMin> … </configuration>
Next, we consider the update procedure for each package separately.
Core
After updating the
WebMarkupMin.Core package,
you need to replace all the namespace connections of
WebMarkupMin.Core.Minifiers
and
WebMarkupMin.Core.Settings
with
WebMarkupMin.Core
in the code of your application.
External CSS minifiers and js code
Unfortunately,
WebMarkupMin.MsAjax and
WebMarkupMin.Yui modules do not support .NET Core, and therefore can only be used in .NET 4.X applications, ASP.NET 4.X web applications and ASP.NET Core web applications created on Based on the "ASP.NET Core Web Application (.NET Framework)" template. This is because the authors of
Microsoft Ajax Minifier and
YUI Compressor for .NET have not ported their libraries to .NET Core. About YUI Compressor for .NET I can not say anything definite, maybe after some time it is still ported. As for Microsoft Ajax Minifier, everything is already very clear: its development was discontinued in March 2015 (after the departure of Ron Logan from Microsoft). But not everything is so bad, because there is a fork of Microsoft Ajax Minifier called
NUglify , which is compatible with. NET Core. The new version of WebMarkupMin has a package based on it -
WebMarkupMin.NUglify .
Ms ajax
After updating the WebMarkupMin.MsAjax package, in your application code, replace all the
WebMarkupMin.MsAjax.Minifiers
and
WebMarkupMin.MsAjax.Settings
namespace
WebMarkupMin.MsAjax.Settings
in
WebMarkupMin.MsAjax
.
YUI
After updating the WebMarkupMin.Yui package, in your application code, replace all
WebMarkupMin.Yui.Minifiers
and
WebMarkupMin.Yui.Settings
namespace
WebMarkupMin.Yui.Settings
in
WebMarkupMin.Yui
.
Nuglify
At the moment, the WebMarkupMin.NUglify API is completely identical to WebMarkupMin.MsAjax. Therefore, when switching from WebMarkupMin.MsAjax to WebMarkupMin.NUglify in the application code, simply replace the
MsAjax
prefixes with
NUglify
.
Extensions for integration with ASP.NET
All packages with ASP.NET extensions for version 1.X (
WebMarkupMin.Web ,
WebMarkupMin.Mvc and
WebMarkupMin.WebForms ) are deprecated and are not used in WebMarkupMin 2.X.
I will list the main innovations in the extensions for ASP.NET:
- Transition to the imperative approach to setting extensions.
- Now you can associate content types (MIME types) with the appropriate markup minifiers.
- Now it is possible to specify the minifikator which pages of the site it should process.
ASP.NET 4.X HTTP modules
To update an ASP.NET web application that uses HTTP modules from the WebMarkupMin.Web package, do the following:
- Remove the WebMarkupMin.Web package.
- Install the WebMarkupMin.AspNet4.HttpModules package.
- Upgrade the remaining old WebMarkupMin packages to version 2.X.
- In the
Web.config
file you need to replace: the WebMarkupMin.Web.HttpModules
namespace with WebMarkupMin.AspNet4.HttpModules
, the assembly name WebMarkupMin.Web
with WebMarkupMin.AspNet4.HttpModules
and the name of the CompressionModule
class on HttpCompressionModule
.
Due to the opportunity to specify to the minifier which pages of the site it should process, HTTP modules
HtmlMinificationModule
and
XhtmlMinificationModule
can now be used together.
ASP.NET 4.X MVC
To update an ASP.NET MVC web application that uses action filters from the WebMarkupMin.Mvc package, you must perform the following steps:
- Remove WebMarkupMin.Mvc and WebMarkupMin.Web packages.
- Install the WebMarkupMin.AspNet4.Mvc package.
- Upgrade the remaining old WebMarkupMin packages to version 2.X.
- In your application code, replace all
WebMarkupMin.Mvc.ActionFilters
namespace WebMarkupMin.Mvc.ActionFilters
with WebMarkupMin.AspNet4.Mvc
.
It is also worth noting that, unlike the old versions of WebMarkupMin, in version 2.X you can apply action filters to controllers:
… using System.Web.Mvc; using WebMarkupMin.AspNet4.Mvc; … namespace WebMarkupMin.Sample.AspNet4.Mvc4.Controllers { [CompressContent] [MinifyHtml] [MinifyXhtml] [MinifyXml] … public class HomeController : Controller { … } }
In addition, in version 2.X, you can apply action filters at the level of the entire web application. To do this, you need to edit the file
App_Start/FilterConfig.cs
as follows:
using System.Web.Mvc; using WebMarkupMin.AspNet4.Mvc; namespace WebMarkupMin.Sample.AspNet4.Mvc4 { public class FilterConfig { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { … filters.Add(new CompressContentAttribute()); filters.Add(new MinifyHtmlAttribute()); filters.Add(new MinifyXhtmlAttribute()); filters.Add(new MinifyXmlAttribute()); … } } }
Also, errors like "The 'MinifyXXXAttribute' attribute can be applied to the 'XXX' controller, that is, the content type.". Now, if the content type of the result of the action does not match the filter, then a minification is simply not applied to this result.
ASP.NET 4.X Web Forms
To upgrade an ASP.NET Web Forms web application that uses the classes of pages or master pages from the WebMarkupMin.WebForms package, follow these steps:
- Remove WebMarkupMin.WebForms and WebMarkupMin.Web packages.
- Install the WebMarkupMin.AspNet4.WebForms package.
- Upgrade the remaining old WebMarkupMin packages to version 2.X.
- In your application code, replace all
WebMarkupMin.WebForms.Pages
and WebMarkupMin.WebForms.MasterPages
namespace WebMarkupMin.WebForms.MasterPages
with WebMarkupMin.AspNet4.WebForms
.
In version 2.X, classes of pages have been added that support only minification (without HTTP compression):
MinifiedHtmlPage
and
MinifiedXhtmlPage
. Also, the corresponding classes appeared for the master pages:
MinifiedHtmlMasterPage
and
MinifiedXhtmlMasterPage
.
ASP.NET Core 1.X
The new version of WebMarkupMin has an extension for ASP.NET Core -
WebMarkupMin.AspNetCore1 package. In terms of functionality, this package resembles WebMarkupMin.AspNet4.HttpModules, only instead of four HTTP modules, this contains one component of the middleware. The main difference of this package from WebMarkupMin.AspNet4.HttpModules is that the modules (capabilities) are connected and configured in one place - in the
Startup.cs
file.
Consider an example of connecting modules:
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using WebMarkupMin.AspNetCore1; namespace TestAspNetCore1 { 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.AddMvc(); } // This method gets called by the runtime. // Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { … app.UseWebMarkupMin(); app.UseMvc(routes => { … }); } } }
In the
ConfigureServices
method, services are registered. Using the
AddWebMarkupMin
extension
AddWebMarkupMin
and its child methods (
AddHtmlMinification
,
AddXmlMinification
and
AddHttpCompression
) we add WebMarkupMin services to the dependency injection container:
- The
AddWebMarkupMin
method registers the following classes as singletons: ThrowExceptionLogger
( ThrowExceptionLogger
interface ILogger
), KristensenCssMinifierFactory
( ICssMinifierFactory
interface ICssMinifierFactory
) and CrockfordJsMinifierFactory
( IJsMinifierFactory
interface IJsMinifierFactory
). By itself, this method does not include a single module (features), for this purpose its child methods serve. For example, if you do not call the AddHtmlMinification
method, then HTML minification will not be available. - The
AddHtmlMinification
method registers the HtmlMinificationManager
class (implementation of the IHtmlMinificationManager
interface) as a singleton. There is also a similar method - AddXhtmlMinification
, which registers the XhtmlMinificationManager
class as an implementation of the IXhtmlMinificationManager
interface. - The
AddXmlMinification
method registers the XmlMinificationManager
class (implementation of the IXmlMinificationManager
interface) as a singleton. - The
AddHttpCompression
method registers the HttpCompressionManager
class (implementation of the IHttpCompressionManager
interface) as a singleton.
In the
Configure
method, the middleware components are registered. Using the
UseWebMarkupMin
extension
UseWebMarkupMin
we add the
WebMarkupMinMiddleware
class to the ASP.NET pipeline. The call to this method must be made immediately before the call to the
UseMvc
method, because the
RouterMiddleware
component ends the call chain.
Thus, we have added HTML minification, XML minification and HTTP compression to our web application. In the “ASP.NET Core Extension Configuration Model” section, we will look at how to configure and override the added services.
Basic configuration model of ASP.NET extensions
Prior to WebMarkupMin 2.X, ASP.NET extensions were configured by editing the
Web.config
file. Now, to configure extensions instead of the declarative approach (using the configuration file), the imperative approach (using program code) is used.
The rejection of the declarative approach was caused by the following reasons:
- Microsoft has been using the imperative approach for several years to customize individual parts of ASP.NET (for example, Web API or Web Optimization Framework).
- ASP.NET Core has only limited support for
Web.config
files (mainly for integration with IIS).
Extensions for ASP.NET 4.X and ASP.NET Core 1.X use different configuration models. In ASP.NET 4.X, extensions are configured through special classes:
WebMarkupMinConfiguration
,
HtmlMinificationManager
,
XhtmlMinificationManager
and
XmlMinificationManager
. In ASP.NET Core 1.X, the extensions configuration is based on the
Microsoft.Extensions.Options framework and uses the following classes:
WebMarkupMinOptions
,
HtmlMinificationOptions
,
XhtmlMinificationOptions
and
XmlMinificationOptions
. However, these configuration models have much in common, and their interfaces and base classes are separated into a separate package -
WebMarkupMin.AspNet.Common .
Basic settings for ASP.NET extensions
The
WebMarkupMinConfiguration
and
WebMarkupMinOptions
inherit the
WebMarkupMinConfigurationBase
class, which has the following properties.
Property | Data type | Default value | Description |
---|
DisableMinification | Boolean | false | Disables markup minification. |
DisableCompression | Boolean | false | Disables HTTP compression of text content. |
MaxResponseSize | Integer | -1 | The maximum size of the HTTP response (in bytes), above which the markup minification is disabled. If the value of this property is -1 , then the size of the HTTP response is not checked. |
DisablePoweredByHttpHeaders | Boolean | false | Disable HTTP headers *-Minification-Powered-By (for example, the X-HTML-Minification-Powered-By: WebMarkupMin ) |
This class and its subclasses are a replacement for the
configuration/webMarkupMin/webExtensions
configuration section from the previous version of WebMarkupMin.
Managers and Markup Minification Options
Minification managers are essentially customizable factories that create instantiations of markup minifiers. But besides that, they also contain logic that allows selectively applying minification (based on the content type and URL).
All classes of managers and markup minification options have the
IncludedPages
and
ExcludedPages
properties, which allow you to include / exclude site pages from being processed by the miniature. These properties are of type
IList<IUrlMatcher>
, and by default contain empty lists, since By default, filtering is disabled and all pages are minified.
There are 3 built-in implementations of the
IUrlMatcher
interface:
ExactUrlMatcher
. The URL is used as a template (for example, new ExactUrlMatcher("/contact")
).RegexUrlMatcher
. The template uses a regular expression that is compatible with the ECMAScript standard (for example, new RegexUrlMatcher(@"^/minifiers/x(?:ht)?ml-minifier$")
).WildcardUrlMatcher
. Wildcard syntax is used (for example, new WildcardUrlMatcher("/minifiers/x*ml-minifier")
). This syntax supports 2 metacharacters: an asterisk ( *
) - matches any character string, even an empty one; and the question mark ( ?
) matches any single character.
By default, all the above implementations of the
IUrlMatcher
interface
IUrlMatcher
not case sensitive. To change this behavior, you need to pass a value of
true
class constructor as the second parameter (for example,
new ExactUrlMatcher("/Contact", true)
).
Manager and HTML Minification Options
The
HtmlMinificationManager
and
HtmlMinificationOptions
have the following common properties:
Property | Data type | Default value | Description |
---|
MinificationSettings | HtmlMinificationSettings | HtmlMinificationSettings class HtmlMinificationSettings | HTML minifiers settings. |
SupportedMediaTypes | ISet<string> | text/html | List of supported content types. |
IncludedPages | IList<IUrlMatcher> | Blank list | Includes pages of the site in the processing of HTML-miniature. |
ExcludedPages | IList<IUrlMatcher> | Blank list | Excludes site pages from being processed by HTML minifiers. |
CssMinifierFactory | ICssMinifierFactory | KristensenCssMinifierFactory class instance | Factory CSS-minifiers. |
JsMinifierFactory | IJsMinifierFactory | Instance of the CrockfordJsMinifierFactory Class | Factory JS-minifiers. |
Manager and XHTML Minification Options
The
XhtmlMinificationManager
and
XhtmlMinificationOptions
have the following common properties:
Property | Data type | Default value | Description |
---|
MinificationSettings | XhtmlMinificationSettings | XhtmlMinificationSettings class XhtmlMinificationSettings | XHTML minifiers settings. |
SupportedMediaTypes | ISet<string> | text/html , application/xhtml+xml | List of supported content types. |
IncludedPages | IList<IUrlMatcher> | Blank list | Includes site pages in the processing of XHTML-minifiers. |
ExcludedPages | IList<IUrlMatcher> | Blank list | Excludes site pages from being processed by an XHTML minifier. |
CssMinifierFactory | ICssMinifierFactory | KristensenCssMinifierFactory class instance | Factory CSS-minifiers. |
JsMinifierFactory | IJsMinifierFactory | Instance of the CrockfordJsMinifierFactory Class | Factory JS-minifiers. |
Manager and XML Minification Options
The
XmlMinificationManager
and
XmlMinificationOptions
have the following common properties:
Property | Data type | Default value | Description |
---|
MinificationSettings | XmlMinificationSettings | XmlMinificationSettings class XmlMinificationSettings | XML minifiers settings. |
SupportedMediaTypes | ISet<string> | application/xml , text/xml , application/xml-dtd , application/xslt+xml , application/rss+xml , application/atom+xml , application/rdf+xml , application/soap+xml , application/wsdl+xml , image/svg+xml , application/mathml+xml , application/voicexml+xml , application/srgs+xml | List of supported content types. |
IncludedPages | IList<IUrlMatcher> | Blank list | Includes pages of the site in the processing of XML-minifiers. |
ExcludedPages | IList<IUrlMatcher> | Blank list | Excludes site pages from being processed by the XML minifier. |
HTTP Compression Manager
The
HttpCompressionManager
class
HttpCompressionManager
also a factory and is responsible for creating instances of GZip and Deflate compressors. But unlike mine managers, it does not have custom properties. Like the minification managers, it contains logic that allows you to selectively apply HTTP compression (only text content is compressed).
The decision on which type of compressor you need to create is made based on the value of the
Accept-Encoding
HTTP header. If the browser supports both types of compression, Deflate is preferred.
ASP.NET 4.X Extensions Configuration Model
The ASP.NET 4.X-extensions are configured through special classes:
WebMarkupMinConfiguration
,
HtmlMinificationManager
,
XhtmlMinificationManager
and
XmlMinificationManager
(these classes are defined in the
WebMarkupMin.AspNet4.Common package).
ASP.NET extension settings
The
WebMarkupMinConfiguration
class in addition to the properties inherited from the
WebMarkupMinConfigurationBase
class also has its own properties:
Property | Data type | Default value | Description |
---|
AllowMinificationInDebugMode | Boolean | false | Enables minification of markup in debug mode. |
AllowCompressionInDebugMode | Boolean | false | Enable HTTP compression of text content in debug mode. |
The debug mode is determined based on the value of the
debug
attribute of the
configuration/system.web/compilation
configuration element from the
Web.config
file:
<?xml version="1.0" encoding="utf-8"?> <configuration> … <system.web> … <compilation debug="false" … /> … </system.web> … </configuration>
By default, in debug mode, no minification and HTTP compression are performed. To enable them, you need to set the above configuration properties to
true
or put web applications in release mode.
Setting Features
Before I talk about the features of setting extensions for ASP.NET 4.X in WebMarkupMin version 2.X, I first give an example of settings based on the
Web.config
file from the previous version:
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSections> … <sectionGroup name="webMarkupMin"> <section name="core" type="WebMarkupMin.Core.Configuration.CoreConfiguration, WebMarkupMin.Core" /> <section name="msAjax" type="WebMarkupMin.MsAjax.Configuration.MsAjaxConfiguration, WebMarkupMin.MsAjax" /> <section name="webExtensions" type="WebMarkupMin.Web.Configuration.WebExtensionsConfiguration, WebMarkupMin.Web" /> </sectionGroup> … </configSections> … <webMarkupMin xmlns="http://tempuri.org/WebMarkupMin.Configuration.xsd"> <core> <html removeRedundantAttributes="true" removeHttpProtocolFromAttributes="true" removeHttpsProtocolFromAttributes="true" /> <xhtml removeRedundantAttributes="true" removeHttpProtocolFromAttributes="true" removeHttpsProtocolFromAttributes="true" /> <xml collapseTagsWithoutContent="true" /> <css defaultMinifier="MsAjaxCssMinifier"> <minifiers> <add name="NullCssMinifier" displayName="Null CSS Minifier" type="WebMarkupMin.Core.Minifiers.NullCssMinifier, WebMarkupMin.Core" /> <add name="KristensenCssMinifier" displayName="Mads Kristensen's CSS minifier" type="WebMarkupMin.Core.Minifiers.KristensenCssMinifier, WebMarkupMin.Core" /> <add name="MsAjaxCssMinifier" displayName="Microsoft Ajax CSS Minifier" type="WebMarkupMin.MsAjax.Minifiers.MsAjaxCssMinifier, WebMarkupMin.MsAjax" /> </minifiers> </css> <js defaultMinifier="MsAjaxJsMinifier"> <minifiers> <add name="NullJsMinifier" displayName="Null JS Minifier" type="WebMarkupMin.Core.Minifiers.NullJsMinifier, WebMarkupMin.Core" /> <add name="CrockfordJsMinifier" displayName="Douglas Crockford's JS Minifier" type="WebMarkupMin.Core.Minifiers.CrockfordJsMinifier, WebMarkupMin.Core" /> <add name="MsAjaxJsMinifier" displayName="Microsoft Ajax JS Minifier" type="WebMarkupMin.MsAjax.Minifiers.MsAjaxJsMinifier, WebMarkupMin.MsAjax" /> </minifiers> </js> <logging defaultLogger="MyLogger"> <loggers> <add name="NullLogger" displayName="Null Logger" type="WebMarkupMin.Core.Loggers.NullLogger, WebMarkupMin.Core" /> <add name="ThrowExceptionLogger" displayName="Throw exception logger" type="WebMarkupMin.Core.Loggers.ThrowExceptionLogger, WebMarkupMin.Core" /> <add name="MyLogger" displayName="My logger" type="TestAspNetMvc5.MyLogger, TestAspNetMvc5" /> </loggers> </logging> </core> <msAjax> <css colorNames="Major" /> <js quoteObjectLiteralProperties="true" /> </msAjax> <webExtensions disableMinificationInDebugMode="false" disableCompressionInDebugMode="false" /> </webMarkupMin> … </configuration>
Next, I will reproduce these settings using the new configuration model.
Configuring ASP.NET 4.X-extensions is a lot like setting up the
Microsoft ASP.NET Web Optimization Framework .
In ASP.NET MVC and Web Forms web applications, WebMarkupMin is configured in
App_Start/WebMarkupMinConfig.cs
:
using System.Collections.Generic; using WebMarkupMin.AspNet.Common; using WebMarkupMin.AspNet.Common.UrlMatchers; using WebMarkupMin.AspNet4.Common; using WebMarkupMin.Core; using WebMarkupMin.MsAjax; namespace TestAspNetMvc5 { public class WebMarkupMinConfig { public static void Configure(WebMarkupMinConfiguration configuration) { configuration.AllowMinificationInDebugMode = true; configuration.AllowCompressionInDebugMode = true; IHtmlMinificationManager htmlMinificationManager = HtmlMinificationManager.Current; htmlMinificationManager.ExcludedPages = new List<IUrlMatcher> { new WildcardUrlMatcher("/minifiers/x*ml-minifier"), new ExactUrlMatcher("/contact") }; htmlMinificationManager.MinificationSettings = new HtmlMinificationSettings { RemoveRedundantAttributes = true, RemoveHttpProtocolFromAttributes = true, RemoveHttpsProtocolFromAttributes = true }; IXhtmlMinificationManager xhtmlMinificationManager = XhtmlMinificationManager.Current; xhtmlMinificationManager.IncludedPages = new List<IUrlMatcher> { new WildcardUrlMatcher("/minifiers/x*ml-minifier"), new ExactUrlMatcher("/contact") }; xhtmlMinificationManager.MinificationSettings = new XhtmlMinificationSettings { RemoveRedundantAttributes = true, RemoveHttpProtocolFromAttributes = true, RemoveHttpsProtocolFromAttributes = true }; IXmlMinificationManager xmlMinificationManager = XmlMinificationManager.Current; xmlMinificationManager.MinificationSettings = new XmlMinificationSettings { CollapseTagsWithoutContent = true }; DefaultCssMinifierFactory.Current = new MsAjaxCssMinifierFactory( new MsAjaxCssMinificationSettings { ColorNames = CssColor.Major } ); DefaultJsMinifierFactory.Current = new MsAjaxJsMinifierFactory( new MsAjaxJsMinificationSettings { QuoteObjectLiteralProperties = true } ); DefaultLogger.Current = new MyLogger(); } } }
Web.config
, –
IncludedPages
ExcludedPages
( ). ,
text/html
2 : HTML-, XHTML-. , , .
, – CSS- JS-. ASP.NET- CSS- JS-, .
1.X 3 :
- .NET-.
- .NET-.
- .
2.X , . 2 :
ICssMinifierFactory
IJsMinifierFactory
. , .. CSS- JS- .
DefaultCssMinifierFactory
DefaultJsMinifierFactory
. , , . HTML- XHTML-
CssMinifierFactory
JsMinifierFactory
:
… using WebMarkupMin.MsAjax; using WebMarkupMin.Yui; namespace TestAspNetMvc5 { public class WebMarkupMinConfig { public static void Configure(WebMarkupMinConfiguration configuration) { … IHtmlMinificationManager htmlMinificationManager = HtmlMinificationManager.Current; … htmlMinificationManager.CssMinifierFactory = new MsAjaxCssMinifierFactory(); htmlMinificationManager.JsMinifierFactory = new MsAjaxJsMinifierFactory(); IXhtmlMinificationManager xhtmlMinificationManager = XhtmlMinificationManager.Current; … xhtmlMinificationManager.CssMinifierFactory = new YuiCssMinifierFactory(); xhtmlMinificationManager.JsMinifierFactory = new YuiJsMinifierFactory(); … } } }
,
App_Start/WebMarkupMinConfig.cs
,
WebMarkupMinConfig.Configure
Global.asax
:
… using System.Web.Routing; using WebMarkupMin.AspNet4.Common; namespace TestAspNetMvc5 { public class MvcApplication : System.Web.HttpApplication { protected void Application_Start() { … RouteConfig.RegisterRoutes(RouteTable.Routes); WebMarkupMinConfig.Configure(WebMarkupMinConfiguration.Instance); } } }
ASP.NET Web Pages
App_Start/WebMarkupMinConfig.cs
Global.asax
—
_AppStart.cshtml
:
@using WebMarkupMin.AspNet.Common @using WebMarkupMin.AspNet.Common.UrlMatchers @using WebMarkupMin.AspNet4.Common @using WebMarkupMin.Core @using WebMarkupMin.MsAjax @using TestAspNetWebPages3 @{ … #region WebMarkupMin configuration WebMarkupMinConfiguration configuration = WebMarkupMinConfiguration.Instance; configuration.AllowMinificationInDebugMode = true; configuration.AllowCompressionInDebugMode = true; IHtmlMinificationManager htmlMinificationManager = HtmlMinificationManager.Current; htmlMinificationManager.ExcludedPages = new List<IUrlMatcher> { new WildcardUrlMatcher("/minifiers/x*ml-minifier"), new ExactUrlMatcher("/contact") }; htmlMinificationManager.MinificationSettings = new HtmlMinificationSettings { RemoveRedundantAttributes = true, RemoveHttpProtocolFromAttributes = true, RemoveHttpsProtocolFromAttributes = true }; IXhtmlMinificationManager xhtmlMinificationManager = XhtmlMinificationManager.Current; xhtmlMinificationManager.IncludedPages = new List<IUrlMatcher> { new WildcardUrlMatcher("/minifiers/x*ml-minifier"), new ExactUrlMatcher("/contact") }; xhtmlMinificationManager.MinificationSettings = new XhtmlMinificationSettings { RemoveRedundantAttributes = true, RemoveHttpProtocolFromAttributes = true, RemoveHttpsProtocolFromAttributes = true }; IXmlMinificationManager xmlMinificationManager = XmlMinificationManager.Current; xmlMinificationManager.MinificationSettings = new XmlMinificationSettings { CollapseTagsWithoutContent = true }; DefaultCssMinifierFactory.Current = new MsAjaxCssMinifierFactory( new MsAjaxCssMinificationSettings { ColorNames = CssColor.Major } ); DefaultJsMinifierFactory.Current = new MsAjaxJsMinifierFactory( new MsAjaxJsMinificationSettings { QuoteObjectLiteralProperties = true } ); DefaultLogger.Current = new MyLogger(); #endregion }
, .. .
ASP.NET Core-
ASP.NET-
WebMarkupMinOptions
,
WebMarkupMinConfigurationBase
, :
Property | | Default value | Description |
---|
AllowMinificationInDevelopmentEnvironment | | false | development-. |
AllowCompressionInDevelopmentEnvironment | | false | HTTP- development-. |
ASP.NET ,
.
ASPNETCORE_ENVIRONMENT
. , 3 , :
Development
,
Staging
Production
.
development- HTTP-.
true
Development
.
, () WebMarkupMin.AspNetCore1. , . 2 ASP.NET: Microsoft.Extensions.Options . « ASP.NET 4.X-»:
using System.Collections.Generic; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using WebMarkupMin.AspNet.Common.UrlMatchers; using WebMarkupMin.AspNetCore1; using WebMarkupMin.Core; using WebMarkupMin.NUglify; namespace TestAspNetCore1 { 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") }; options.MinificationSettings = new HtmlMinificationSettings { RemoveRedundantAttributes = true, RemoveHttpProtocolFromAttributes = true, RemoveHttpsProtocolFromAttributes = true }; }) .AddXhtmlMinification(options => { options.IncludedPages = new List<IUrlMatcher> { new WildcardUrlMatcher("/minifiers/x*ml-minifier"), new ExactUrlMatcher("/contact") }; options.MinificationSettings = new XhtmlMinificationSettings { RemoveRedundantAttributes = true, RemoveHttpProtocolFromAttributes = true, RemoveHttpsProtocolFromAttributes = true }; }) .AddXmlMinification(options => { options.MinificationSettings = new XmlMinificationSettings { CollapseTagsWithoutContent = true }; }) .AddHttpCompression() ; services.AddSingleton<ICssMinifierFactory>(new NUglifyCssMinifierFactory( new NUglifyCssMinificationSettings { ColorNames = CssColor.Major } )); services.AddSingleton<IJsMinifierFactory>(new NUglifyJsMinifierFactory( new NUglifyJsMinificationSettings { QuoteObjectLiteralProperties = true } )); services.AddSingleton<WebMarkupMin.Core.Loggers.ILogger, MyLogger>(); … } … } }
, , , , .
, . , WebMarkupMin.MsAjax — WebMarkupMin.NUglify, .NET Core.
Links
- WebMarkupMin GitHub
- WebMarkupMin 2.X
- «WebMarkupMin HTML Minifier – HTML- .NET»
- NUglify
- « ASP.NET5, №3 — -»
- «Dependency Injection» ASP.NET Core.
- «Working with Multiple Environments» ASP.NET Core.