How to Minify HTML output from ASP.NET MVC
By FoxLearn 7/4/2024 9:09:48 AM 362
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
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 the 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.
- How to use CORS in ASP.NET Core
- How to Send Emails in ASP.NET Core
- How to Run Background Tasks in ASP.NET Core with Hosted Services
- Implementing Scheduled Background Tasks in ASP.NET Core with IHostedService
- Creating an Web API in ASP.NET Core
- 8 Essential Tips to Protect Your ASP.NET Core Application from Cyber Threats
- 10 Common Mistakes ASP.NET Developers Should Avoid
- How to Upload Files Using C# ASP.NET FileUpload Control