Sunday, March 12, 2017

Difference between Response.Write and Response.Output.Write

Today we will learn about difference between Response.Write () and Response.Output.Write ()

Basically Response.Write() method is used to write the response back to customer browser as http response output stream. It only accepts string as parameter.

However the good approach to show http response output stream we should first encode the input from user by using htmlencode or with urlencode because it is a major risk to give http response without encoding.

Response.Write() basically response object come from httpresponse. In original it is HttpContext.Current.Response.Write(). HttpResponse contains a property Output that belongs to TextWriter and TextWriter itself also have Write() method that give response in string.Format pattern.

public TextWriter Output
{
    get
    {
        return this._writer;
    }
}

Normally

Response.Write ("Hi Concat inside!"+" Yes done");     // only string is ok.

When we use

Response.Write("Today day is {0} at {1:d}", "Monday", DateTime.Now);//error because it does not support conversion

Response.Output.Write("Today is {0} at {1:d}", "Monday", DateTime.Now);//it will convert date and will be shown as string and displayed.

So Response.Write() on show the string but latter will support other types also and give you formatted output.

No comments:

Post a Comment