Skip to content

ASP.NET Extensions

Taritsyn edited this page May 18, 2021 · 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).

Managers and options

All classes of markup minification and compression managers (and their options) have IncludedPages and ExcludedPages properties, that allow to include/exclude site pages from processing by corresponding markup minifier or compressor. 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)).

Starting with version 2.4.0, by default, only the GET requests are minified and compressed. You can change this behavior by using the SupportedHttpMethods property:

.SupportedHttpMethods = new HashSet<string> { "GET", "POST" };

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.
SupportedHttpStatusCodes ISet<int> 200 List of supported HTTP status codes.
SupportedHttpMethods ISet<string> GET List of supported HTTP methods.
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.
SupportedHttpStatusCodes ISet<int> 200 List of supported HTTP status codes.
SupportedHttpMethods ISet<string> GET List of supported HTTP methods.
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.
SupportedHttpStatusCodes ISet<int> 200 List of supported HTTP status codes.
SupportedHttpMethods ISet<string> GET List of supported HTTP methods.
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 GZipCompressorFactory class,
Instance of DeflateCompressorFactory class
List of HTTP compressor factories. Position of factory instance in the list determines the priority of compression algorithm.
SupportedHttpStatusCodes ISet<int> 200 List of supported HTTP status codes.
SupportedHttpMethods ISet<string> GET List of supported HTTP methods.
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.
IncludedPages IList<IUrlMatcher> Empty list List of URL matchers, which is used to include pages to processing by compressor.
ExcludedPages IList<IUrlMatcher> Empty list List of URL matchers, which is used to exclude pages from processing by compressor.

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. 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. For example, there is a module, that adds support for the Brotli algorithm in ASP.NET extensions - WebMarkupMin.AspNet.Brotli.