Altaf Khatri
Altaf Khatri



RSS feed for http://www.AltafKhatri.com website. Twitter account of http://www.AltafKhatri.com website.
∗Recently Added - How to take care of Betta fighter fish and increase life span?  10/30/2013


Find me on web

 LinkedIn

 Facebook

 Twitter

Remove Unnecessary Space And Line Break From MVC ASP.NET


When the size of the HTML page that is rendered to the end user browser is really big, it has few disadvantages:
  1. The page loads slow
  2. Unnecessary space in the html don't serve any purpose to the page, then why have anything unncessary.

For these reasons it is better to get rid of the white spaces and line breaks.

To see if the page is rendering unnecessary white spaces and line breaks right click on the page and see the source of the page. The html rendered, if it has spaces and line breaks then the solution below is one of the solution what you are looking for to get rid of this problem.

There are multiple ways to handle this but the solution below is my favorite. The other solutions are:

  1. Compression of HTML at the IIS response level.
  2. Compression of HTML using GZip compression at the application level.

The solution below is applicable to the ASP.NET MVC framework view page but it can be used in any other ASP.NET application.

It is better to put the below code in the master page because you then don't have to worry about every page rendering unnecessary spaces and line breaks.

Steps:

  1. Create a codebehind file for the master page by following the instructions below:
    1. Right click on the folder containing the master file. Select Add New Item option.
    2. Select class item template and name the class as the name of the master file with .master.cs as extension.
    If the name of the master file is: Website.Master then the name of the codebehind should be: Website.Master.cs This should associate a master and code behind file in the visual studio hierarchy.
  2. Now Inherit the Website.Master.cs in the Website.Master This can be done by changing the Master directive:
    Before:
    <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

    After:
    <%@ Master Language="C#" Inherits="AltafKhatri.Views.Shared.WebSite" %>

    where:
    AltafKhatri.Views.Shared : is the namespace of the master code behind file and WebSite : is the name of the master codebehind file.

  3. In the codebehind of the master file use the following references:
    1. using System.Web.UI;
    2. using System.Text.RegularExpressions;

    Paste the code below in the master page code behind:

    namespace AltafKhatri.Views.Shared

    {

    public partial class WebSite : System.Web.Mvc.ViewMasterPage

    {

    private static readonly Regex REGEX_BETWEEN_TAGS = new Regex(@">\s+<", RegexOptions.Compiled);

    private static readonly Regex REGEX_LINE_BREAKS = new Regex(@"\n\s+", RegexOptions.Compiled);

    protected override void Render(HtmlTextWriter writer)

    {

    using (HtmlTextWriter htmlwriter = new HtmlTextWriter(new System.IO.StringWriter()))

    {

    base.Render(htmlwriter);

    string html = htmlwriter.InnerWriter.ToString();

    html = REGEX_BETWEEN_TAGS.Replace(html, "> <");

    html = REGEX_LINE_BREAKS.Replace(html, string.Empty);

    writer.Write(html.Trim());

    }

    }

    }

    }

This should take care of rendering the html without unnecessary spaces and line breaks.

For the inquisitives: The removal code is placed in the Render of the master page as it is the last chance to change the html content before it's sent to the browser. We want to remove the unnecessary spaces and line breaks as the last thing in the response life cycle.

I know MVC framework discourages having code behind but in this case I have made it the only exception. It has not hurt my MVC architecture and the performance of the website.

I will be glad to hear the comments and suggestions about different ways to handle this in MVC.

Reference(s) for this article: http://madskristensen.net/post/Remove-whitespace-from-your-pages.aspx





SiteMap  |  Contact Me