Aug 7 2007

Extend System.Web.HttpResponse with a WriteLine method

Category: C# 3.0Bil@l @ 11:50

While working with Console applications, you always use Console.WriteLine. I always got bothered why the HttpResponse has only Write or WriteFile. Why can't we have WriteLine? Many times you need to print something on a web page and also print a break line, and you always had to append a "<br>".

Now, with the Extension Methods that ship as part of C# 3.0, we can add the WriteLine method as follows. Create a new Class Library and place this code inside it:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.CodeDom;
using System.Reflection;
using System.IO;

namespace Utils
{
    public static class CodeSuiteExtensionMethods
    {
        #region HttpResponse Extension Methods
        /// <summary>
        /// This method prints the contents of a string s followed
        /// by a new line.
        /// </summary>
        /// <param name="response">An <see cref="System.Web.HttpResponse" object/></param>
        /// <param name="s">A <see cref="=System.String"/> to be printed</param>
        public static void WriteLine(this System.Web.HttpResponse response, string s)
        {
            // Call default Write method
            System.Web.HttpContext.Current.Response.Write(s);
           
            // Add a new line
            System.Web.HttpContext.Current.Response.Write("<br>");
        }
        /// <summary>
        /// This method prints the contents of a character ch followed
        /// by a new line.
        /// </summary>
        /// <param name="response">An <see cref="System.Web.HttpResponse" object/></param>
        /// <param name="ch">A <see cref="=System.char"/> to be printed</param>
        public static void WriteLine(this System.Web.HttpResponse response, char ch)
        {
            // Call default Write method
            System.Web.HttpContext.Current.Response.Write(ch);
           
            // Add a new line
            System.Web.HttpContext.Current.Response.Write("<br>");
        }
        /// <summary>
        /// This method prints the contents of a buffer of characters followed
        /// by a new line.
        /// </summary>
        /// <param name="response">An <see cref="System.Web.HttpResponse" object/></param>
        /// <param name="buffer">An array of characters</param>
        /// <param name="index">An <see cref="System.Integer"/> to start printing from</param>
        /// <param name="count">An <see cref="=System.Integer"/> that states number of characters to print</param>
        /// <param name="s">A <see cref="=System.String" to be printed/></param>
        public static void WriteLine(this System.Web.HttpResponse response, char[] buffer, int index, int count)
        {
            // Call default Write method
            System.Web.HttpContext.Current.Response.Write(buffer, index, count);
           
            // Add a new line
            System.Web.HttpContext.Current.Response.Write("<br>");
        }
        /// <summary>
        /// This method prints the ToString of an object obj followed
        /// by a new line.
        /// </summary>
        /// <param name="response">An <see cref="System.Web.HttpResponse" object/></param>
        /// <param name="obj">A <see cref="=System.Object"/> to be printed</param>
        public static void WriteLine(this System.Web.HttpResponse response, object obj)
        {
            // Call default Write method
            System.Web.HttpContext.Current.Response.Write(obj);
           
            // Add a new line
            System.Web.HttpContext.Current.Response.Write("<br>");
        }
        #endregion
    }
}

I have create Extension Methods for all the overloads of the Write method. What I do is simply call the original Write method, then add another call to write a break line.

How to use it? Add this namespace to your current page:

using Utils; // name of the namespace containing the extension methods.

Before importing the namespace make sure you Add a Reference to that namespace.

Now, in your code you can do something:

Response.WriteLine("Hello world"); // This will print the statement Hello world with a break line!!

Even if you wanted to use the Response.WriteLine same as Console.WriteLine to customize it with parameters, you can do something as:

Response.WriteLine(string.Format("Hello {0}", "Bilal"));

 

Hope this helps,
Regards

 

Tags:

Comments are closed