How to Minify HTML output from ASP.NET MVC
By FoxLearn 7/4/2024 9:09:48 AM 302
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.
- Unable to resolve service for type 'Microsoft.AspNetCore.Identity.RoleManager'
- ASP.NET MVC Responsive Templates Free Download
- How to upload file in ASP.NET MVC
- How to Create Contact Form Flat Responsive in ASP.NET MVC
- HTTP Error 500.30 ASP.NET Core app failed to start
- How to Use IExceptionHandler in ASP.NET Core
- How to custom exception handling in ASP.NET Core
- How to check if HttpPostedFileBase is an image