How to Minify HTML using WebMarkupMin in ASP.NET MVC
By FoxLearn 2/18/2024 1:45:01 AM 45
This post shows you how to minify HTML at runtime in ASP.NET MVC to make your web page load faster
To do that you need to install WebMarkupMin.AspNet4.Mvc library from Nuget Package Manager or you can download it directly from https://github.com/Taritsyn/WebMarkupMin/wiki/WebMarkupMin:-ASP.NET-4.X-MVC
WebMarkupMin.AspNet4.Mvc is a library allows you to minify HTML in ASP.NET MVC or ASP.NET MVC Core
Open FilterConfig class, then add the config to minify HTML and XML content as below
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new CompressContentAttribute()); filters.Add(new MinifyHtmlAttribute()); filters.Add(new MinifyXmlAttribute()); }
You can add atributtes to minify HTML for each Action that you want to minify
[CompressContent] [MinifyHtml] public ActionResult Index() { return View(); } [CompressContent] [MinifyXhtml] public ActionResult Contact() { return View(); }
You can also add atributtes to Controller instead of Action
[CompressContent] [MinifyHtml] [MinifyXml] public class HomeController : Controller { public ActionResult Index() { return View(); } public ActionResult Contact() { return View(); } }
Run your project->Right click on your web page ->Inspect->Network tab. You can see a reduction in the number of data transfers
- How to fix Assets file 'project.assets.json' not found
- Could not load file or assembly 'netstandard, Version=2.0.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies
- How to fix "InvalidOperationException: No service for type 'Microsoft.AspNetCore.Identity.UserManager'"
- How to fix 'IMvcBuilder' does not contain a definition for 'AddNewtonsoftJson'
- ASP.NET Core: How to Create RDLC Report in ASP.NET Core
- ASP.NET MVC: Free Responsive Templates
- How to fix 'DbContextOptionsBuilder' does not contain a definition for 'UseSqlServer'
- How to set cookie ValidationInterval in ASP.NET Core
Categories
Popular Posts
Visual Studio: Auto increment version
04/01/2024
What is ARM architecture?
04/01/2024