ASP.NET 4.X MVC

dron edited this page Oct 16, 2018 · 1 revision

WebMarkupMin.AspNet4.Mvc package is suitable for use in web applications written in ASP.NET MVC versions 3, 4 and 5.

WebMarkupMin.AspNet4.Mvc contains 4 classes of action filters:

  1. MinifyHtmlAttribute. Produces a minification of the action result by using the HTML minification manager.
  2. MinifyXhtmlAttribute. Produces a minification of the action result by using the XHTML minification manager.
  3. MinifyXmlAttribute. Produces a minification of the action result by using the XML minification manager.
  4. CompressContentAttribute. Produces a HTTP compression of the action result with text content type by using the HTTP compression manager.
using System.Web.Mvc;

using WebMarkupMin.AspNet4.Mvc;
…

namespace WebMarkupMin.Sample.AspNet4.Mvc4.Controllers
{
    public class HomeController : Controller
    {
        …
        [CompressContent]
        [MinifyHtml]
        [OutputCache(CacheProfile = "CacheCompressedContent5Minutes")]
        public ActionResult Index()
        {
            …
        }

        …

        [CompressContent]
        [MinifyXhtml]
        [OutputCache(CacheProfile = "CacheCompressedContent5Minutes")]
        public ActionResult Contact()
        {
            …
        }

        [CompressContent]
        [MinifyXml]
        [OutputCache(CacheProfile = "CacheCompressedContent5Minutes")]
        public ActionResult Sitemap()
        {
            …
        }
        …
    }
}

Because markup minification and HTTP compression require time and server resources, then we cache the result of their work by using OutputCacheAttribute.

Unlike older versions of WebMarkupMin, in version 2.X you can apply the action filters to controllers:

using System.Web.Mvc;

using WebMarkupMin.AspNet4.Mvc;
…

namespace WebMarkupMin.Sample.AspNet4.Mvc4.Controllers
{
    [CompressContent]
    [MinifyHtml]
    [MinifyXml]
    …
    public class HomeController : Controller
    {
        …
    }
}

In addition, in version 2.X you can apply the action filters at the level of whole application. For this you need to edit the App_Start/FilterConfig.cs file 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 MinifyXmlAttribute());
            …
        }
    }
}
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.