Minify HTML content in Asp.net and MVC

If you have website in ASP.net MVC, here is good news for you. Now you can minify HTML content from just simple command line tool. Special thanks to Deanhume

Generally we use Google PageSpeed Insights to check website performance, as it provides tips to improve overall performance and page load time below are the image of that website.

Check website performance in this website
Check website performance in this website

We also use GTmetrix to check website performance it will provide almost every information which we have required to enhance our web application performance.

Check website performance in this website
Check website performance in this website

If you are Asp.net developer, So there are some options are available to minify your HTML content is the Asp.net HTML Minifier.

The Asp.net HTML Minifier is a simple command line tool that remove white spaces in your HTML. The great thing about this tool it is useful for Asp.net web-forms, Asp.net MVC Razor view and MVC web-forms and plain HTML files. You just provide the path of your website it simply minify all your content.

Just download HTML Minifier command line tool from here

After download this project along with source code extract that project and go to tool folder in that folder you can get HtmlMinifier.exe we can use this .exe in our visual studio so at the time of publish it will provide the minify version of HTML.

So lets start the implementation of HtmlMinifier.exe:

Step 1: Place this .exe file in our project solution, So you can excess this .exe file and every time when you publish you website so it will minify your content.

Step 2: Open solution explorer in visual studio and go to properties under this Publish Profiles is present open that file, below is an screen shot for your reference.

Solution Explorer in Visual Studio
Solution Explorer in Visual Studio

Open that file with extension of .pubxml and placed this code after property group.

Don’t forget to replace your htmlminifier.exe file path with your original file path.

pubxml file
pubxml file

That’s it you are ready to publish your profile,┬áSave your pubxml file and publish your project so it will run htmlminifier.exe and only minify the HTML files that get deployed. If you see your output window, so it will look like this.

Minify Output window
Minify Output window

The secret to getting this to work is “CopyAllFilesToSingleFolderForPackage” step in the built.

If you have any query or suggestion, So i’ll be happy to help you.

KEEP CALM and READ ON!!!!.

Ankur Omar