---
title: "Minifying HTML for ASP.NET MVC - Muhammad Rehan Saeed"
description: "How much bandwidth does minifying HTML save. Minifying HTML in ASP.NET MVC 5 is hard work. Minifying HTML should be a built in feature of ASP.NET Core."
canonical_url: "https://rehansaeed.com/minifying-html-for-asp-net-mvc/"
last_updated: "2015-08-06T00:00:00+00:00"
meta:
  author: "Muhammad Rehan Saeed"
  keywords: "ASP.NET MVC,HTML,Minification,Razor"
  "og:description": "How much bandwidth does minifying HTML save. Minifying HTML in ASP.NET MVC 5 is hard work. Minifying HTML should be a built in feature of ASP.NET Core."
  "og:image": "https://rehansaeed.com/_og/s/o_045bead8cdca776e.png"
  "og:image:alt": "Minifying HTML for ASP.NET MVC - Muhammad Rehan Saeed"
  "og:image:height": "630"
  "og:image:type": image/png
  "og:image:width": "1200"
  "og:title": "Minifying HTML for ASP.NET MVC"
  "twitter:image": "https://rehansaeed.com/_og/s/o_045bead8cdca776e.png"
  "twitter:image:alt": "Minifying HTML for ASP.NET MVC - Muhammad Rehan Saeed"
---

> Index of every page on this site: [llms.txt](https://rehansaeed.com/llms.txt)

# [Minifying HTML for ASP.NET MVC](https://rehansaeed.com/minifying-html-for-asp-net-mvc/)

Posted 6 August 2015 - **4 min read**

![The Internet-Is-A-Series-Of-Tubes](https://rehansaeed.com/_ipx/f_jpg&s_2560x1440/images/hero/The-Internet-Is-A-Series-Of-Tubes-1366x768.jpg)

Using Razor comments or blocks of code can cause extra carriage returns to appear in the generated HTML. This has been a problem in all versions if ASP.NET MVC for a while now.

```html
<p>Paragraph 1</p>
@* Razor Comment *@
<p>Paragraph 2</p>
```

The above code generates the following HTML. You can imagine that with a lot of comments or code blocks you get a lot of ugly blank lines appearing in your mark-up.

```html
<p>Paragraph 1</p>

<p>Paragraph 2</p>
```

Ideally it should generate the HTML below without any blank lines. If you really wanted a blank line to appear, you could add one yourself before the comment.

```html
<p>Paragraph 1</p>
<p>Paragraph 2</p>
```

The main problem with the above is that it makes your HTML look ugly and hard to follow. You often end up with several blank lines, which breaks up the flow of the mark-up.

Also, given that every ASP.NET MVC site on the internet has this problem and probably contains at least two Razor comments and maybe a for-loop in the code somewhere, that is a lot of wasted extra bandwidth.

So I made [this](https://github.com/aspnet/Razor/issues/428) suggestion for the next version of ASP.NET Core, to change the behaviour to the expected one above and it got accepted!

# How Much Bandwidth Was Saved

So how much bandwidth has this single change saved the internet? That's the question I asked myself. According to the [httparchive.org](http://httparchive.org/trends.php), the average request is made up of around 57KB of HTML. If we assume that each page contains two comments and maybe a for-loop, that's six carriage returns (Two sets of), twelve characters or twelve bytes of wasted bandwidth. If we assume that all sites are using GZip compression, then we can make a conservative estimate of around six bytes of wasted bandwidth per request.

![Average HTML Transfer Size Over a Request Chart](https://rehansaeed.com/_ipx/f_png&s_2560x1280/content-images/posts/2015/minifying-html-for-asp-net-mvc/images/Average-HTML-Transfer-Size-Over-a-Request-Chart.png)

[Cisco](http://www.cisco.com/c/en/us/solutions/collateral/service-provider/visual-networking-index-vni/VNI_Hyperconnectivity_WP.html) forecasts that global IP traffic will pass the Zettabyte (1000 Exabytes) threshold by the end of 2016. If the the average transfer size per request is [2162 KB](http://httparchive.org/trends.php#numurls); and only 57 KB is HTML, we can work out that 257465 Terabytes of of the worlds internet traffic per year is HTML.

![Average Total Transfer Size Over a Request Chart](https://rehansaeed.com/_ipx/f_png&s_2560x1280/content-images/posts/2015/minifying-html-for-asp-net-mvc/images/Average-Total-Transfer-Size-Over-a-Request-Chart.png)

According to [w3techs.com](http://w3techs.com/technologies/history_overview/programming_language), 16.7% of all sites on the internet use ASP.NET as of 1st August 2015, lets assume half of those (8.35%) will use ASP.NET Core in a few years time. So, we can say that very roughly 21498 Terabytes of the worlds bandwidth is consumed on ASP.NET HTML requests per year.

If the average wasted bandwidth is six bytes out of a total of 57 KB per HTML request, then we come to a grand total of around 2.3 Terabytes of bandwidth saved per year. I must admit, that's a lot of bandwidth but I still thought it would be a lot more than that.

# HTML Minification

An even better solution would be to minify the HTML. There are solutions like [Web Markup Min](http://webmarkupmin.codeplex.com/) for ASP.NET Core but it works at runtime and is a little involved to set up, so all but the most determined developers use this feature.

Then there is [Dean Hume's](http://www.deanhume.com/Home/BlogPost/a-simple-html-minifier-for-asp-net/2097) compile time minifier which sounded perfect. In Dean's post he gets savings of around 20-30% by minifying his HTML. If we applied a conservative 20% saving to all ASP.NET Core HTML requests, that would work out to be a 4300 Terabyte saving in global bandwidth per year!

So far, I've only mentioned the bandwidth saving but downloading smaller HTML files will also mean quicker page load times. HTML is the first thing a browser downloads before it can go off and download all the other CSS, JavaScript, fonts and images a site needs to display a page. Making this download smaller, is a small but effective way to get pages up quicker.

These days, MVC has things like CSS and JavaScript minification built in as standard. To squeeze out even more performance, HTML minification is the next logical step.

So I made [this](https://github.com/aspnet/Razor/issues/423) suggestion for ASP.NET Core to implement Dean's compile time minification of Razor views by default. So far, it's not been taken up but I live in hope and write this blog post to show how cool a feature it is.

Please do go and post your support for this feature. We don't necessarily need to use Dean's technique, minifying HTML could just as easily be a Grunt or Gulp task.

# Conclusions

This post contains huge leaps of guess work and estimation. I hope my maths is up to scratch but I would not be surprised if I was off by a decimal point or two. Still, we are talking huge numbers here and I hope I've convinced you that minifying HTML is worth the effort.

## Web Mentions

[What's this?](https://en.wikipedia.org/wiki/Webmention)

### 0 Linked

### 0 Replies

## GitHub Comments

### 6 Comments

[Comment on GitHub](https://github.com/RehanSaeed/rehansaeed.github.io/issues/89)

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed "RehanSaeed")

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Josh Olson](https://camo.githubusercontent.com/82b003e639a8c818ccaf603adf10ae20e8a1ff52099881d176ce35f6fc0b6294/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f34326562626436393463363666366530333030373862643031353066363838643f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/82b003e639a8c818ccaf603adf10ae20e8a1ff52099881d176ce35f6fc0b6294/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f34326562626436393463363666366530333030373862643031353066363838643f733d393626643d6d6d26723d67) **Josh Olson** commented on **2016-02-19 02:55:16**

I'm pretty sure your conclusion is right: you're off by a decimal point or two. One reason is the arithmetic and another is conceptual.

First, the arithmetic. Forgive me if I followed you incorrectly but this is what I'm seeing. We got 1 zettabyte (1000 exabytes) of traffic. Since we're using the SI zetta instead of the IEC zebi, I'll stick to that throughout. 1 zettabyte = 1,000,000,000 terabytes. Using your ratio of 57KB / 2162KB, that's 26,364,477.3 terabytes. Compare that to your 257,465 terabytes. What am I missing? ... I don't think anything based on the information you're providing me. If I'm right, your methodology would conclude even more bandwidth would be wasted. However...

Conceptually, you missed something huge. Global IP Traffic != Global HTTP Traffic. I highly doubt HTTP traffic comes anywhere close to BitTorrent traffic, Netflix, YouTube (HTTP but the ratio is probably closer to 57KB/100MB), video games, etc.

Regardless, I'm all for minifying. :-)

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed "RehanSaeed")

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2016-02-19 10:21:38**

> I'm pretty sure your conclusion is right: you're off by a decimal point or two. One reason is the arithmetic and another is conceptual.
>
> First, the arithmetic. Forgive me if I followed you incorrectly but this is what I'm seeing. We got 1 zettabyte (1000 exabytes) of traffic. Since we're using the SI zetta instead of the IEC zebi, I'll stick to that throughout. 1 zettabyte = 1,000,000,000 terabytes. Using your ratio of 57KB / 2162KB, that's 26,364,477.3 terabytes. Compare that to your 257,465 terabytes. What am I missing? ... I don't think anything based on the information you're providing me. If I'm right, your methodology would conclude even more bandwidth would be wasted. However...
>
> Conceptually, you missed something huge. Global IP Traffic != Global HTTP Traffic. I highly doubt HTTP traffic comes anywhere close to BitTorrent traffic, Netflix, YouTube (HTTP but the ratio is probably closer to 57KB/100MB), video games, etc.
>
> Regardless, I'm all for minifying. :-)

Yes the numbers are **very** rough. I had to go out and discover certain basic facts which I could base my math on. It turns out that it's pretty hard to do and we just don't know what global internet usage really amounts to.

My intention with this blog post was to spur the ASP.NET developers into action and show how amazing it would be to add a HTML minification feature, make it super easy to use and turn it on by default. So far, I haven't got any really good vibes but I'm hoping the next version will answer this question.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed "RehanSaeed")

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Flo](https://camo.githubusercontent.com/a1130047cc2f6b11803094e8344179babf80a4dd9f2c8fe88a6db6986d9c256c/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f35383063353933336365653132396138343337613063363365333738626433353f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/a1130047cc2f6b11803094e8344179babf80a4dd9f2c8fe88a6db6986d9c256c/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f35383063353933336365653132396138343337613063363365333738626433353f733d393626643d6d6d26723d67) **[Flo](http://konto-erstellen.de/)** commented on **2016-03-08 11:41:37**

How is guaranteed that these tools/plugins/libs/etc. are not breaking complex views, esp. Razor syntax can get very "special"?  
 Using such thingy without having it written on your own makes you testing the whole app/website again, i'd say?

Nontheless, the performance gains esp. for mobile sites are awesome...

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed "RehanSaeed")

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Muhammad Rehan Saeed](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/8a07049df41b8a3839a3f4342f09ea8e6e7fa0065667687f04beca2d088a1dca/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32623266393030653361633066353535316462646537323864336262343935343f733d393626643d6d6d26723d67) **[Muhammad Rehan Saeed](https://rehansaeed.com/)** commented on **2016-03-08 12:10:59**

> How is guaranteed that these tools/plugins/libs/etc. are not breaking complex views, esp. Razor syntax can get very "special"?  
>  Using such thingy without having it written on your own makes you testing the whole app/website again, i'd say?
>
> Nontheless, the performance gains esp. for mobile sites are awesome...

That's why this feature is totally experimental. That said, it really is doing very little and does 80% of the minification work, while still not breaking anything in my testing. The minified razor is there for you to look at, so you could make your own adjustments to fix something if it was not quite right, although that is not ideal. There are a lot of other more complex things that can be done for HTML minification, such as changing `checked="true"` to just `checked`.

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed "RehanSaeed")

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![Tom Pažourek](https://camo.githubusercontent.com/12e061ff48015257c4c46f417f6f0ded43639b32ec8e68446ac5eb949dec08be/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f66386466323263373734653765643434393138393939373937313531623831323f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/12e061ff48015257c4c46f417f6f0ded43639b32ec8e68446ac5eb949dec08be/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f66386466323263373734653765643434393138393939373937313531623831323f733d393626643d6d6d26723d67) **Tom Pažourek** commented on **2017-11-17 08:36:00**

I recently built a very trivial and tiny HTML minifier for Razor that's easy to set up (just change the factory in the Web.config file), works in compile-time, and doesn't do any special minification magic. It just basically replaces multiple spaces with one. From my testing, whitespace is the biggest issue in HTML files, and this fixes just that (so side-effects should be minimal). If anybody's interested in some easy-to-use and simple solution, check it out (it's literally less than 100 lines of code):

https://github.com/tompazourek/RazorHtmlMinifier.Mvc5

[![RehanSaeed](https://avatars.githubusercontent.com/u/2270364?v=4)](https://github.com/RehanSaeed "RehanSaeed")

### [RehanSaeed](https://github.com/RehanSaeed)

Owner

12 May 2020

[![SEO](https://camo.githubusercontent.com/1cda9f6a0500b25be5097d488549fa4cee70d1bc0076b8371656694c05fe1afe/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32323333336364383334643338613364623234616361623261663031333838333f733d393626643d6d6d26723d67)](https://camo.githubusercontent.com/1cda9f6a0500b25be5097d488549fa4cee70d1bc0076b8371656694c05fe1afe/68747470733a2f2f7777772e67726176617461722e636f6d2f6176617461722f32323333336364383334643338613364623234616361623261663031333838333f733d393626643d6d6d26723d67) **[SEO](http://www.tabnakit.com/)** commented on **2018-04-07 08:06:04**

Great, thank you for sharing this article.

```json
{"@context":"https://schema.org","@graph":[{"@id":"https://rehansaeed.com/#website","@type":"WebSite","description":"Software Developer at Microsoft, YouTuber, Open Source Contributor and Blogger","inLanguage":"en-GB","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/","author":{"@id":"https://rehansaeed.com/#identity"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://rehansaeed.com/?search={search_term_string}"},"query-input":"required name=search_term_string"}],"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/minifying-html-for-asp-net-mvc/#webpage","@type":"WebPage","dateModified":"2015-08-06T00:00:00+00:00","datePublished":"2015-08-06T00:00:00+00:00","description":"How much bandwidth does minifying HTML save. Minifying HTML in ASP.NET MVC 5 is hard work. Minifying HTML should be a built in feature of ASP.NET Core.","name":"Minifying HTML for ASP.NET MVC","url":"https://rehansaeed.com/minifying-html-for-asp-net-mvc/","breadcrumb":{"@id":"https://rehansaeed.com/minifying-html-for-asp-net-mvc/#breadcrumb"},"isPartOf":{"@id":"https://rehansaeed.com/#website"},"potentialAction":[{"@type":"ReadAction","target":["https://rehansaeed.com/minifying-html-for-asp-net-mvc/"]}]},{"@id":"https://rehansaeed.com/#identity","@type":"Person","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com/about/","image":{"@id":"https://rehansaeed.com/#/schema/image/1"},"sameAs":["https://github.com/RehanSaeed","https://www.linkedin.com/in/muhammad-rehan-saeed","https://stackoverflow.com/users/1212017/muhammad-rehan-saeed","https://www.youtube.com/c/MuhammadRehanSaeed","https://www.twitch.tv/rehansaeeduk"]},{"@id":"https://rehansaeed.com/#publisher","@type":"Organization","logo":"https://rehansaeed.com/images/schema/Publisher-600x60.png","name":"Muhammad Rehan Saeed","url":"https://rehansaeed.com"},{"@id":"https://rehansaeed.com/minifying-html-for-asp-net-mvc/#article","@type":"Article","dateModified":"2015-08-06T00:00:00+00:00","datePublished":"2015-08-06T00:00:00+00:00","description":"How much bandwidth does minifying HTML save. Minifying HTML in ASP.NET MVC 5 is hard work. Minifying HTML should be a built in feature of ASP.NET Core.","headline":"Minifying HTML for ASP.NET MVC","inLanguage":"en-GB","url":"https://rehansaeed.com/minifying-html-for-asp-net-mvc/","author":{"@id":"https://rehansaeed.com/#identity"},"image":[{"@type":"ImageObject","url":"https://rehansaeed.com/images/hero/The-Internet-Is-A-Series-Of-Tubes-1366x768.jpg","caption":"The Internet-Is-A-Series-Of-Tubes","width":"1366","height":"768"}],"isPartOf":{"@id":"https://rehansaeed.com/minifying-html-for-asp-net-mvc/#webpage"},"keywords":["ASP.NET MVC","HTML","Minification","Razor"],"mainEntityOfPage":{"@id":"https://rehansaeed.com/minifying-html-for-asp-net-mvc/#webpage"},"publisher":{"@id":"https://rehansaeed.com/#publisher"}},{"@id":"https://rehansaeed.com/minifying-html-for-asp-net-mvc/#breadcrumb","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","name":"Blog","item":"https://rehansaeed.com/","position":1},{"@type":"ListItem","name":"Minifying HTML for ASP.NET MVC","item":"https://rehansaeed.com/minifying-html-for-asp-net-mvc/","position":2}]},{"@id":"https://rehansaeed.com/#/schema/image/1","@type":"ImageObject","contentUrl":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png","inLanguage":"en-GB","url":"https://rehansaeed.com/images/author/Muhammad-Rehan-Saeed/Logo-512x512.png"}]}
```

## Sitemap

See the full [sitemap](https://rehansaeed.com/sitemap.md) for all pages.
