How to Minify HTML output from ASP.NET MVC

This post shows you how to minify HTML in ASP.NET MVC or ASP.NET Web Forms. To make your site load faster, you should minimize downloads of resources.

You can use https://developers.google.com/speed/pagespeed/insights/?hl=en to  check the performance of your websites

First you need to download Minifier Html tool from https://github.com/deanhume/html-minifier

This is a simple command line tool to minify your HTML, Razor views & Web Forms views. By minifying your HTML on bigger web pages, you'll save on bytes that your clients need to download.

Open Properties->PublishProfiles->Open your pubxml extension file

Add HtmlMinifier.exe to your publishing profile

<?xml version="1.0" encoding="utf-8"?>
<!--
This file is used by the publish/package process of your Web project. You can customize the behavior of this process
by editing this MSBuild file. In order to learn more about this please visit https://go.microsoft.com/fwlink/?LinkID=208121. 
-->
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>FileSystem</WebPublishMethod>
    <PublishProvider>FileSystem</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish />
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <PrecompileBeforePublish>True</PrecompileBeforePublish>
    <EnableUpdateable>True</EnableUpdateable>
    <DebugSymbols>False</DebugSymbols>
    <WDPMergeOption>DonotMerge</WDPMergeOption>
    <ExcludeApp_Data>True</ExcludeApp_Data>
    <publishUrl>D:\Publish\Test</publishUrl>
    <DeleteExistingFiles>True</DeleteExistingFiles>
  </PropertyGroup>
  <Target Name="CustomAction" AfterTargets="CopyAllFilesToSingleFolderForPackage">
    <Message Text="Minifying files....." />
    <Exec Command="D:\Publish\HtmlMinifier.exe $(_PackageTempDir)" IgnoreExitCode="true" />
  </Target>
</Project>

Note: Copy HtmlMinifier.exe to your publishing project directory, then publish your project

For example: Original html file

<h2>
    foxlearn.com
</h2>
<ul>
    <li>@Html.ActionLink("Home", "Index", "Home")</li>
</ul>

After Minifying the html file

<h2>foxlearn.com</h2><ul><li>@Html.ActionLink("Home", "Index", "Home")</li></ul>

I hope you can optimize your site's performance by using the HtmlMinifier tool