HttpResponse.Write Method

Definition

Writes information to an HTTP response output stream.

Overloads

Write(Char)

Writes a character to an HTTP response output stream.

Write(Object)

Writes an Object to an HTTP response stream.

Write(String)

Writes a string to an HTTP response output stream.

Write(Char[], Int32, Int32)

Writes an array of characters to an HTTP response output stream.

Write(Char)

Writes a character to an HTTP response output stream.

C#
public void Write(char ch);

Parameters

ch
Char

The character to write to the HTTP output stream.

Examples

The following example creates a series of constants that are written to an ASP.NET page using the Write method. The code calls this version of the Write method to write individual character constants to the page.

ASP.NET (C#)
    <%

        // Create a character array.
    char[] charArray = {'H', 'e', 'l', 'l', 'o', ',', ' ', 
                           'w', 'o', 'r', 'l', 'd'};

        // Write a character array to the client.
        Response.Write(charArray, 0, charArray.Length);

        // Write a single characher.
        Response.Write(';');

        // Write a sub-section of a character array to the client.
        Response.Write(charArray, 0, 5);
// <snippet6>
        // Write an object to the client.
        object obj = (object)13;
        Response.Write(obj);
// </snippet6>

    %>

Applies to

.NET Framework 4.8.1 i druge verzije
Proizvod Verzije
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Write(Object)

Writes an Object to an HTTP response stream.

C#
public void Write(object obj);

Parameters

obj
Object

The Object to write to the HTTP output stream.

Applies to

.NET Framework 4.8.1 i druge verzije
Proizvod Verzije
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Write(String)

Writes a string to an HTTP response output stream.

C#
public void Write(string s);

Parameters

s
String

The string to write to the HTTP output stream.

Examples

The following example echoes the client's name back to the client's browser. The HtmlEncode method strips any malicious script and invalid characters that may have been submitted in the UserName input field.

C#
Response.Write("Hello " + Server.HtmlEncode(Request.QueryString["UserName"]) + "<br>");
    

Remarks

Dynamically generated HTML pages can introduce security risks if input received from Web clients is not validated either when it is received from a client or when it is transmitted back to a client. Malicious script that is embedded in input submitted to a Web site and later written back out to a client can appear to be originating from a trusted source. This security risk is referred to as a cross-site scripting attack. You should always validate data that is received from a client when it will be transmitted from your site to client browsers.

Moreover, whenever you write out as HTML any data that was received as input, you should encode it using a technique such as HtmlEncode or UrlEncode to prevent malicious script from executing. This technique is useful for data that was not validated when it was received.

When you encode or filter data, you must specify a character set for your Web pages so that your filter can identify and remove any byte sequences that do not belong to that set (such as nonalphanumeric sequences) and could potentially have malicious script embedded in them.

Applies to

.NET Framework 4.8.1 i druge verzije
Proizvod Verzije
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Write(Char[], Int32, Int32)

Writes an array of characters to an HTTP response output stream.

C#
public void Write(char[] buffer, int index, int count);

Parameters

buffer
Char[]

The character array to write.

index
Int32

The position in the character array where writing starts.

count
Int32

The number of characters to write, beginning at index.

Examples

The following example creates a series of constants that are written to an ASP.NET page using the Write method. The code calls this version of the Write method to write individual character constants to the page.

ASP.NET (C#)
    <%

        // Create a character array.
    char[] charArray = {'H', 'e', 'l', 'l', 'o', ',', ' ', 
                           'w', 'o', 'r', 'l', 'd'};

        // Write a character array to the client.
        Response.Write(charArray, 0, charArray.Length);

        // Write a single characher.
        Response.Write(';');

        // Write a sub-section of a character array to the client.
        Response.Write(charArray, 0, 5);
// <snippet6>
        // Write an object to the client.
        object obj = (object)13;
        Response.Write(obj);
// </snippet6>

    %>

Applies to

.NET Framework 4.8.1 i druge verzije
Proizvod Verzije
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1