Skip to content

ASP.NET 4.X Extensions

Andrey Taritsyn edited this page Aug 14, 2018 · 11 revisions

Configuration of ASP.NET 4.X extensions are made through special classes: WebMarkupMinConfiguration, HtmlMinificationManager, XhtmlMinificationManager, XmlMinificationManager and HttpCompressionManager.

WebMarkupMin configuration

WebMarkupMinConfiguration class in addition to properties, that inherited from WebMarkupMinConfigurationBase class, also has its own properties:

Property name Data type Default value Description
AllowMinification­InDebugMode Boolean false Flag for whether to allow markup minification during debugging.
AllowCompression­InDebugMode Boolean false Flag for whether to allow HTTP compression of content during debugging.

Debug mode is determined based on the value of debug attribute of configuration/system.web/compilation configuration element from Web.config file:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    …
    <system.web>
        …
        <compilation debug="false" … />
        …
    </system.web>
    …
</configuration>

By default, in debug mode is not performed a markup minification and HTTP compression. To enable these features, you need to set the value equal to true to the above configuration properties or switch a web application in release mode.

Specificity of configuration

Configuring of WebMarkupMin in many ways resembles configuration of the Microsoft ASP.NET Web Optimization Framework.

In ASP.NET MVC and Web Forms applications configuring of WebMarkupMin maked in App_Start/WebMarkupMinConfig.cs file:

using System.Collections.Generic;

using WebMarkupMin.AspNet.Brotli;
using WebMarkupMin.AspNet.Common;
using WebMarkupMin.AspNet.Common.Compressors;
using WebMarkupMin.AspNet.Common.UrlMatchers;
using WebMarkupMin.AspNet4.Common;
using WebMarkupMin.Core;
using WebMarkupMin.MsAjax;
using WebMarkupMin.Yui;

namespace WebMarkupMin.Sample.AspNet4.Mvc4
{
    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")
            };
            HtmlMinificationSettings htmlMinificationSettings =
                htmlMinificationManager.MinificationSettings;
            htmlMinificationSettings.RemoveRedundantAttributes = true;
            htmlMinificationSettings.RemoveHttpProtocolFromAttributes = true;
            htmlMinificationSettings.RemoveHttpsProtocolFromAttributes = true;
            htmlMinificationManager.CssMinifierFactory =
                new MsAjaxCssMinifierFactory();
            htmlMinificationManager.JsMinifierFactory =
                new MsAjaxJsMinifierFactory();

            IXhtmlMinificationManager xhtmlMinificationManager = 
                XhtmlMinificationManager.Current;
            xhtmlMinificationManager.IncludedPages = new List<IUrlMatcher>
            {
                new WildcardUrlMatcher("/minifiers/x*ml-minifier"),
                new ExactUrlMatcher("/contact")
            };
            XhtmlMinificationSettings xhtmlMinificationSettings = 
                xhtmlMinificationManager.MinificationSettings;
            xhtmlMinificationSettings.RemoveRedundantAttributes = true;
            xhtmlMinificationSettings.RemoveHttpProtocolFromAttributes = true;
            xhtmlMinificationSettings.RemoveHttpsProtocolFromAttributes = true;
            xhtmlMinificationManager.CssMinifierFactory =
                new YuiCssMinifierFactory();
            xhtmlMinificationManager.JsMinifierFactory =
                new YuiJsMinifierFactory();

            IXmlMinificationManager xmlMinificationManager = 
                XmlMinificationManager.Current;
            XmlMinificationSettings xmlMinificationSettings =
                xmlMinificationManager.MinificationSettings;
            xmlMinificationSettings.CollapseTagsWithoutContent = true;

            IHttpCompressionManager httpCompressionManager =
                HttpCompressionManager.Current;
            httpCompressionManager.CompressorFactories = new List<ICompressorFactory>
            {
                new BrotliCompressorFactory(),
                new DeflateCompressorFactory(),
                new GZipCompressorFactory()
            };
        }
    }
}

In order for these configuration settings to take effect, you must also add the WebMarkupMinConfig.Configure method call in the Global.asax file:

using System.Web;using System.Web.Routing;

using WebMarkupMin.AspNet4.Common;

namespace WebMarkupMin.Sample.AspNet4.Mvc4
{
    public class MvcApplication : HttpApplication
    {
        protected void Application_Start()
        {
            …
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            WebMarkupMinConfig.Configure(WebMarkupMinConfiguration.Instance);}
    }
}

In ASP.NET Web Pages sites instead of the App_Start/WebMarkupMinConfig.cs and Global.asax files is used only one file - _AppStart.cshtml:

@using System.Collections.Generic

@using WebMarkupMin.AspNet.Brotli
@using WebMarkupMin.AspNet.Common
@using WebMarkupMin.AspNet.Common.Compressors
@using WebMarkupMin.AspNet.Common.UrlMatchers
@using WebMarkupMin.AspNet4.Common
@using WebMarkupMin.Core
@using WebMarkupMin.MsAjax
@using WebMarkupMin.Yui

@{
    …
    WebMarkupMinConfiguration configuration =
        WebMarkupMinConfiguration.Instance;
    configuration.AllowMinificationInDebugMode = true;
    configuration.AllowCompressionInDebugMode = true;

    IHtmlMinificationManager htmlMinificationManager = 
        HtmlMinificationManager.Current;
    htmlMinificationManager.ExcludedPages = new List<IUrlMatcher>
    {
        new ExactUrlMatcher("/Contact")
    };
    HtmlMinificationSettings htmlMinificationSettings = 
        htmlMinificationManager.MinificationSettings;
    htmlMinificationSettings.RemoveRedundantAttributes = true;
    htmlMinificationSettings.RemoveHttpProtocolFromAttributes = true;
    htmlMinificationSettings.RemoveHttpsProtocolFromAttributes = true;
    htmlMinificationManager.CssMinifierFactory =
        new MsAjaxCssMinifierFactory();
    htmlMinificationManager.JsMinifierFactory =
        new MsAjaxJsMinifierFactory();

    IXhtmlMinificationManager xhtmlMinificationManager = 
        XhtmlMinificationManager.Current;
    xhtmlMinificationManager.IncludedPages = new List<IUrlMatcher>
    {
        new ExactUrlMatcher("/Contact")
    };
    XhtmlMinificationSettings xhtmlMinificationSettings =
        xhtmlMinificationManager.MinificationSettings;
    xhtmlMinificationSettings.RemoveRedundantAttributes = true;
    xhtmlMinificationSettings.RemoveHttpProtocolFromAttributes = true;
    xhtmlMinificationSettings.RemoveHttpsProtocolFromAttributes = true;
    xhtmlMinificationManager.CssMinifierFactory =
        new YuiCssMinifierFactory();
    xhtmlMinificationManager.JsMinifierFactory =
        new YuiJsMinifierFactory();

    IXmlMinificationManager xmlMinificationManager =
        XmlMinificationManager.Current;
    XmlMinificationSettings xmlMinificationSettings = 
        xmlMinificationManager.MinificationSettings;
    xmlMinificationSettings.CollapseTagsWithoutContent = true;

    IHttpCompressionManager httpCompressionManager =
        HttpCompressionManager.Current;
    httpCompressionManager.CompressorFactories = new List<ICompressorFactory>
    {
        new BrotliCompressorFactory(),
        new DeflateCompressorFactory(),
        new GZipCompressorFactory()
    };}

If in the current instances of HtmlMinificationManager and XhtmlMinificationManager classes explicitly not defined values of CssMinifierFactory and JsMinifierFactory properties, then its values will be taken from DefaultCssMinifierFactory and DefaultJsMinifierFactory classes. Сurrent values of DefaultCssMinifierFactory and DefaultJsMinifierFactory classes you can change in App_Start/WebMarkupMinConfig.cs or _AppStart.cshtml file:

DefaultCssMinifierFactory.Current = new YuiCssMinifierFactory();
DefaultJsMinifierFactory.Current = new MsAjaxJsMinifierFactory();

Similarly, you can change the logger, that is used by default:

DefaultLogger.Current = new MyLogger();