Skip to content

ASP.NET Extensions

Andrey Taritsyn edited this page Mar 8, 2017 · 15 revisions

Prior to WebMarkupMin version 2.0, configuring of ASP.NET extensions were made by using the Web.config file. Now during configuration instead of the declarative approach (using the configuration file) is used in the imperative approach (using the program code).

The rejection of the declarative approach was due to the following reasons:

  1. Microsoft for several years, uses an imperative approach for configuration of individual parts of ASP.NET (for example, Web API or Web Optimization Framework).
  2. ASP.NET Core has only limited support of 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 configuring of extensions are made through special classes: WebMarkupMinConfiguration, HtmlMinificationManager, XhtmlMinificationManager, XmlMinificationManager and HttpCompressionManager. In ASP.NET Core 1.X configuration of extension is based on the Options framework and using the following classes: WebMarkupMinOptions, HtmlMinificationOptions, XhtmlMinificationOptions, XmlMinificationOptions and HttpCompressionOptions. However, these configuration models have a lot in common.

WebMarkupMin сommon configuration

WebMarkupMinConfiguration and WebMarkupMinOptions classes inherit WebMarkupMinConfigurationBase class, which has the following properties:

Property name Data type Default value Description
DisableMinification Boolean false Flag for whether to disable markup minification.
DisableCompression Boolean false Flag for whether to disable HTTP compression of content.
MaxResponseSize Int32 -1 Maximum size of the response (in bytes), in excess of which disables the minification of markup. If this property is set equal to -1, then checking of the response size is not performed.
DisablePoweredBy­HttpHeaders Boolean false Flag for whether to disable the *-Minification-Powered-By HTTP headers (e.g. X-HTML-Minification-Powered-By: WebMarkupMin).

Markup minification managers and options

All classes of markup minification managers and options have IncludedPages and ExcludedPages properties, that allow to include/exclude site pages from processing by corresponding markup minifier. These properties are of type IList<IUrlMatcher>, and by default contains the empty lists, i.e. by default filtering is disabled.

There are 3 built-in implementations of IUrlMatcher interface:

  1. ExactUrlMatcher. As a pattern used the URL (e.g., new ExactUrlMatcher("/contact")).
  2. RegexUrlMatcher. As a pattern used a ECMAScript standard compliant regular expression (e.g., new RegexUrlMatcher(@"^/minifiers/x(?:ht)?ml-minifier$")).
  3. WildcardUrlMatcher. Used a pattern, that supports Wildcard syntax (e.g., new WildcardUrlMatcher("/minifiers/x*ml-minifier")). This syntax supports 2 special characters: * (match any number of any characters) and ? (match exactly one character).

By default all of the above implementations of IUrlMatcher interface during matching is not case-sensitive. To change this behavior you need to pass to constructor of class as the second parameter value equals to true (e.g., new ExactUrlMatcher("/Contact", true)).

HTML minification manager and options

HtmlMinificationManager and HtmlMinificationOptions classes have the following common properties:

Property name Data type Default value Description
MinificationSettings HtmlMinification­Settings Instance of HtmlMinification­Settings class HTML minification settings.
SupportedMediaTypes ISet<string> text/html List of supported media types.
IncludedPages IList<IUrlMatcher> Empty list List of URL matchers, which is used to include pages to processing by HTML minifier.
ExcludedPages IList<IUrlMatcher> Empty list List of URL matchers, which is used to exclude pages from processing by HTML minifier.
GenerateStatistics Boolean false Flag for whether to allow generate minification statistics (available through the logger).
CssMinifierFactory ICssMinifierFactory Instance of KristensenCss­MinifierFactory class CSS minifier factory.
JsMinifierFactory IJsMinifierFactory Instance of CrockfordJs­MinifierFactory class JS minifier factory.

XHTML minification manager and options

XhtmlMinificationManager and XhtmlMinificationOptions classes have the following common properties:

Property name Data type Default value Description
MinificationSettings XhtmlMinification­Settings Instance of XhtmlMinification­Settings class XHTML minification settings.
SupportedMediaTypes ISet<string> text/html, application/xhtml+xml List of supported media types.
IncludedPages IList<IUrlMatcher> Empty list List of URL matchers, which is used to include pages to processing by XHTML minifier.
ExcludedPages IList<IUrlMatcher> Empty list List of URL matchers, which is used to exclude pages from processing by XHTML minifier.
GenerateStatistics Boolean false Flag for whether to allow generate minification statistics (available through the logger).
CssMinifierFactory ICssMinifierFactory Instance of KristensenCss­MinifierFactory class CSS minifier factory.
JsMinifierFactory IJsMinifierFactory Instance of CrockfordJs­MinifierFactory class JS minifier factory.

XML minification manager and options

XmlMinificationManager and XmlMinificationOptions classes have the following common properties:

Property name Data type Default value Description
MinificationSettings XmlMinification­Settings Instance of XmlMinification­Settings class XML minification 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 media types.
IncludedPages IList<IUrlMatcher> Empty list List of URL matchers, which is used to include pages to processing by XML minifier.
ExcludedPages IList<IUrlMatcher> Empty list List of URL matchers, which is used to exclude pages from processing by XML minifier.
GenerateStatistics Boolean false Flag for whether to allow generate minification statistics (available through the logger).

HTTP compression manager and options

HttpCompressionManager and HttpCompressionOptions classes have the following common properties:

Property name Data type Default value Description
CompressorFactories IList<ICompressorFactory> Instance of DeflateCompressorFactory class,
Instance of GZipCompressorFactory class
List of HTTP compressor factories. Position of factory instance in the list determines the priority of compression algorithm.
SupportedMediaType­Predicate Func<string, bool> null The delegate that determines whether the media-type is supported. If the delegate is not specified, then to check the media-type uses a IsTextBasedMediaType method of MediaTypeHelpers class.

HttpCompressionManager class is responsible for compression of text content by using GZip or Deflate algorithm. Decision about which algorithm to use depends on the value of Accept-Encoding HTTP header. If browser supports both algorithms, then preference is given to algorithm, which is higher in the list (value of CompressorFactories property).

In principle, you have the possibility to use other compression algorithms (for example, Brotli). To do this, you need to write your own implementations of the ICompressor and ICompressorFactory interfaces. After that add an instance of the implemented factory to top of the list, which is defined in CompressorFactories property.