37

What are the ways by which we can reduce the size of the HTML Response sent by an asp.net application?

I am using Controls which are not owned by me and it produces output with white spaces. I am interested in Minifying the entire HTML output of the page just like how google does (View source www.google.com) to improve the timing.

Is there any Utility classes available for ASP.NET which can do this stuff for me?

Chris Pietschmann
  • 29,502
  • 35
  • 121
  • 166
Ramesh
  • 13,043
  • 3
  • 52
  • 88

6 Answers6

32

There is no need to do it at run time. Because it can be done at compile time.

Details: http://omari-o.blogspot.com/2009/09/aspnet-white-space-cleaning-with-no.html

thorn0
  • 9,362
  • 3
  • 68
  • 96
20

Try HTTP module as described here: http://madskristensen.net/post/a-whitespace-removal-http-module-for-aspnet-20

gius
  • 9,289
  • 3
  • 33
  • 62
10

For Microsoft .NET platform there is a library called the WebMarkupMin, which produces the minification of HTML code. For each ASP.NET framework has its own module:

Documentation is available at - http://webmarkupmin.codeplex.com/documentation

Andrey Taritsyn
  • 1,286
  • 11
  • 26
  • WebMarkupMin is not able to minify razor code, but it can minify the output HTML code. – Andrey Taritsyn Feb 24 '15 at 17:28
  • so how do you use it? I see you can download and install for asp.net web forms but after that what? http://www.nuget.org/packages/WebMarkupMin.AspNet4.WebForms/ – franko_camron Sep 24 '16 at 14:45
  • WebMarkupMin moved to [GitHub](https://github.com/Taritsyn/WebMarkupMin). If you have used old versions of WebMarkupMin, then I recommend to first read [“How to upgrade applications to version 2.X”](https://github.com/Taritsyn/WebMarkupMin/wiki/How-to-upgrade-applications-to-version-2.X) section of the documentation. – Andrey Taritsyn Sep 24 '16 at 15:28
  • 1
    @franko_camron You need to read [“ASP.NET Extensions”](https://github.com/Taritsyn/WebMarkupMin/wiki/ASP.NET-Extensions), [“ASP.NET 4.X Extensions”](https://github.com/Taritsyn/WebMarkupMin/wiki/ASP.NET-4.X-Extensions) and [“WebMarkupMin: ASP.NET 4.X Web Forms”](https://github.com/Taritsyn/WebMarkupMin/wiki/WebMarkupMin:-ASP.NET-4.X-Web-Forms) sections of the documentation. – Andrey Taritsyn Sep 24 '16 at 15:31
6

I want to comment on Thorn's suggestion (but I'm new to stack overflow).

  1. The linked code (omari-o.blogspot.com) doesn't support MVC4, and although the code is open source it cannot easily be upgraded because of braking changes between MVC3 and MVC4.

  2. There might be whitespaces written to the http result at runtime, only the developer of the actual site can know that. Thus static minification of template files (aspx) is not foolproof at all. Dynamic minification, which is suggested by gius, should be used to guarantee that whitespaces are removed correctly, and unfortunately this will incur a runtime computation cost. If code dynamically writes spaces to the output, it will have to be removed dynamically.

Some dude
  • 61
  • 1
  • 1
3

The accepted answer does not work with MVC 4, so here is a similar lib that minifies at build-time https://github.com/jitbit/HtmlOptimizerMvc4

Alex from Jitbit
  • 53,710
  • 19
  • 160
  • 149
2

Just adding another option I do not see listed here, which is the one I was recommended using:

Html minifier command line tool

Usage: here and here

There is an issue, however, with this tool: it leaves single line (//) comments, and it causes problems for Razor parsing, since a single line comment placed within a C# block like the following:

@{
  ... 
  ...
  // anything
  ...
}

will cause the minification output rest of the line, from this point on, to be ignored by the Razor parser, which will thus raise an error stating there it could not find the closing "}" for the block.

My workaround for this issue was to completely removing these comments from the output. This way it works. To do that, simply remove the RegexOptions.SingleLine from line 145:

htmlContents = Regex.Replace(htmlContents, @"//(.*?)\r?\n", ""/*, RegexOptions.Singleline*/);
Veverke
  • 9,208
  • 4
  • 51
  • 95
  • 1
    This issue should be fixed in the latest version - https://github.com/deanhume/html-minifier – Deano Mar 18 '15 at 17:29