---
title: "Compress or Remove WhiteSpace before deploying in Asp.NET MVC"  
description: "In a normal webpage there is a lot of whitespace. Compressed requests and responses always increase the performance of sites. Nowadays, almost all lat"  
author: "AVADHESH PATEL"  
published: 2013-03-06  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/457/compress-or-remove-whitespace-before-deploying-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 2 minutes  

---

# Compress or Remove WhiteSpace before deploying in Asp.NET MVC

In a normal webpage there is a lot of whitespace. Compressed [requests and responses](https://www.mindstick.com/forum/159694/how-to-handle-http-requests-and-responses-in-a-dot-net-core-api-controller) always [increase](https://www.mindstick.com/articles/12959/are-you-struggling-to-increase-the-profit-of-your-liquor-store) the performance of sites. Nowadays, almost all latest browsers support compression. By getting free of all the extra whitespace in the HTML you can save up to 25% of the total weight of the page in kilo bytes and make rendering in the browser faster.\

Example: Retrieve the HTML before sending it to the browser

This can be done in either the Render method of a Page or MasterPage or it can be done in an HttpModule. In [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) you have no Render method to override and you need to remove the whitespace from an HttpModule.

```
protected override void Render(HtmlTextWriter writer){  using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))  {    base.Render(htmlwriter);    string html = htmlwriter.InnerWriter.ToString();    // Trim the whitespace from the 'html' variable    writer.Write(html);  }}
```

##### Example: Remove whitespace

Here we need to use two [regular expressions](https://www.mindstick.com/forum/159366/how-to-test-regular-expressions-in-sql-without-using-database-table) to identify the areas of whitespace that are safe to remove.

private [static readonly](https://answers.mindstick.com/qa/93919/what-are-the-uses-of-const-readonly-static-and-static-readonly-keywords-in-c-sharp-and-also-differentiate-with-example) Regex RegexBetweenTags = new Regex(@">(?! )\s+", RegexOptions.Compiled);

private static readonly Regex RegexLineBreaks = new Regex(@"([\n\s])+?(?<= {2,})<",RegexOptions.Compiled);

This method uses the two regular expressions to remove any whitespace from a string of HTML.

```
public static string RemoveWhitespaceFromHtml(string html){  html = RegexBetweenTags.Replace(html, ">");  html = RegexLineBreaks.Replace(html, "<");   return html.Trim();}
```

You can be [remove whitespaces](https://www.mindstick.com/interview/33832/how-to-remove-whitespaces-from-string-in-java) by using “[Bundling and Minification](https://www.mindstick.com/articles/1086/bundling-and-minification-in-asp-dot-net-mvc-4)” in ASP.NET project. You can read and see demo on “Bundling and Minification” from below URL

http://www.mindstick.com/Articles/2d3da74a-94da-45fe-b361-97263cd725f2/?Bundling%20and%20Minification%20in%20ASP%20NET%20MVC%204

---

Original Source: https://www.mindstick.com/blog/457/compress-or-remove-whitespace-before-deploying-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
