How to Minify HTML using WebMarkupMin in ASP.NET MVC
By Tan Lee Published on Jul 04, 2024 679
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 is a .NET library that helps in minifying various types of markup, including HTML, CSS, and JavaScript.
Here’s how you can use it specifically for HTML minification in an ASP.NET MVC application:
Open FilterConfig
class, then add the config to minify HTML and XML content as shown below.
1 2 3 4 5 6 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[CompressContent] [MinifyHtml] public ActionResult Index() { return View(); } [CompressContent] [MinifyXhtml] public ActionResult Contact() { return View(); } |
You can also add atributtes to Controller instead of Action
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
[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. Now, you can see a reduction in the number of data transfers.
Through this example, you can effectively integrate and use WebMarkupMin to minify HTML in your ASP.NET MVC application, helping to optimize page load times and reduce bandwidth usage.
- Essential Tips for Securing Your ASP.NET Website
- Top Security Best Practices for ASP.NET
- Boost Your ASP.NET Core Website Performance with .NET Profiler
- The name 'Session' does not exist in the current context
- Implementing Two-Factor Authentication with Google Authenticator in ASP.NET Core
- How to securely reverse-proxy ASP.NET Core
- How to Retrieve Client IP in ASP.NET Core Behind a Reverse Proxy
- Only one parameter per action may be bound from body in ASP.NET Core