0

Has anyone got any way to Minify HTML on a ASP.net WEBPAGES site. I have the .JS and .Css Minifed but cant fine anything for the HTML.

I have found along of options for MVC and webforms, I have also tried puting these onto the Webpages framwork but none of them have worked.

I hope someone can help. Remember webpages not MVC

  • 1
    Possible duplicate of [Minify Html output of ASP.NET Application](http://stackoverflow.com/questions/255008/minify-html-output-of-asp-net-application) – Mehdi Dehghani Sep 23 '16 at 06:39
  • Hi @MedhdiDehghani this is not a duplicate, i have tried all the suggestions on that post, some of them aren't even available any more. – Matthew Mccall Sep 23 '16 at 10:34
  • Hi, how about [How to minify aspx pages](http://stackoverflow.com/questions/3829659/how-to-minify-aspx-pages) – Mehdi Dehghani Sep 23 '16 at 11:28

2 Answers2

0

Yes. I had this done a couple of weeks ago for mine. In MVC there is a nuget for minifying cshtml files but considering webforms, here is the fix. There is an open source htmlminifier available to minify html.
1) Please clone this project and run it to create an exe inside the bin folder

htmlminifier exe

2) Right click your project and click 'Unload Project' 3) Right click on the unloaded project and choose edit .csproj 4) Add this target at the end of the file

<Target Name="AfterBuild" AfterTargets="">
    <Message Text="Minifying files.....$(DirectoryGroups)" />
    <Exec Command="c:\Users\Admin\htmlminifier.exe C:\Users\ADMIN\Desktop\htmlminifier" IgnoreExitCode="true" />
  </Target>

5) Reload the project
6) Build and publish your website. The minification is done. Before minification enter image description here

After minification

enter image description here

CodeCaster
  • 131,656
  • 19
  • 190
  • 236
staticvoidmain
  • 753
  • 6
  • 13
  • Hi staticvoidmain, – Matthew Mccall Sep 23 '16 at 07:38
  • Hi staticvoidmain, Thank you for your help, i will try this, but i am using WebPages framework not WebForms. Al tho they all shear a lot of similarities there are differences. The info above is detailed tho so i will giv this ago. thank you. – Matthew Mccall Sep 23 '16 at 08:40
  • @MatthewMccall - It is suggested to use both minification and gzipping to get the best performance. [Here](http://madskristensen.net/post/effects-of-gzipping-vs-minifying-html-files) are the suggestions. – staticvoidmain Sep 29 '16 at 20:42
0

There is an article here on how to do it https://blogs.msdn.microsoft.com/rickandy/2012/08/15/adding-web-optimization-to-a-web-pages-site/

In the _AppStart.cshtml add code like

@using System.Web.Optimization;

@{
     var bundles = BundleTable.Bundles;

     bundles.UseCdn = true;   //enable CDN support

    //add link to jquery on the CDN
    var jqueryCdnPath = "http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.1.min.js";

    bundles.Add(new ScriptBundle("~/bundles/jquery", 
                jqueryCdnPath).Include(
                "~/Scripts/jquery-{version}.js"));

    bundles.Add(new StyleBundle("~/Content/css").Include("~/Content/site.css"));

}

Then in your web page

@using System.Web.Optimization;
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>@Page.Title - My ASP.NET Web Page</title>    

        @Styles.Render("~/Content/css", "~/Content/css");    

        <link href="~/favicon.ico" rel="shortcut icon" type="image/x-icon" />    

        @Scripts.Render("~/bundles/jquery");

        <meta name="viewport" content="width=device-width" />
    </head>
Craig
  • 34,658
  • 33
  • 108
  • 191