HttpResponse.Write 方法

定義

將資訊寫入 HTTP 回應輸出資料流。

多載

Write(Char)

將字元寫入 HTTP 回應輸出資料流。

Write(Object)

Object 寫入 HTTP 回應資料流。

Write(String)

將字串寫入 HTTP 回應輸出資料流。

Write(Char[], Int32, Int32)

將字元的陣列寫入 HTTP 回應輸出資料流。

Write(Char)

將字元寫入 HTTP 回應輸出資料流。

public:
 void Write(char ch);
public void Write (char ch);
member this.Write : char -> unit
Public Sub Write (ch As Char)

參數

ch
Char

要傳送至 HTTP 輸出資料流的字元。

範例

下列範例會使用 Write 方法建立一系列常數,這些常數會寫入至 ASP.NET 網頁。 程式碼會呼叫這個版本的 Write 方法,以將個別字元常數寫入頁面。

    <%

        // 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>

    %>
      <%
         Dim charArray As Char() = {"H"c, "e"c, "l"c, "l"c, "o"c, ","c, " "c, _
                                 "w"c, "o"c, "r"c, "l"c, "d"c}
         ' Write a character array to the client.
         Response.Write(charArray, 0, charArray.Length)

         ' Write a single character.
         Response.Write(";"c)

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

適用於

Write(Object)

Object 寫入 HTTP 回應資料流。

public:
 void Write(System::Object ^ obj);
public void Write (object obj);
member this.Write : obj -> unit
Public Sub Write (obj As Object)

參數

obj
Object

要寫入 HTTP 輸出資料流的 Object

適用於

Write(String)

將字串寫入 HTTP 回應輸出資料流。

public:
 void Write(System::String ^ s);
public void Write (string s);
member this.Write : string -> unit
Public Sub Write (s As String)

參數

s
String

要寫入 HTTP 輸出資料流的字串。

範例

下列範例會將用戶端的名稱回應回用戶端的瀏覽器。 方法 HtmlEncode 會移除任何可能已在輸入欄位中提交的 UserName 惡意腳本和無效字元。

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

備註

如果從 Web 用戶端接收的輸入或從用戶端傳輸回用戶端時,動態產生的 HTML 頁面可能會造成安全性風險。 內嵌在提交至網站的輸入中且稍後寫回給用戶端的惡意腳本,可能似乎源自信任的來源。 此安全性風險稱為跨網站腳本攻擊。 當從您的網站傳輸到用戶端瀏覽器時,您應該一律驗證從用戶端接收的資料。

此外,每當您寫出為 HTML 時,任何收到作為輸入的資料,都應該使用 或 UrlEncode 之類的 HtmlEncode 技術進行編碼,以防止惡意腳本執行。 這項技術適用于未在收到資料時驗證的資料。

當您編碼或篩選資料時,您必須指定網頁的字元集,讓篩選可以識別和移除不屬於該集合的任何位元組序列 (,例如非虛構序列) ,而且可能內嵌惡意腳本。

適用於

Write(Char[], Int32, Int32)

將字元的陣列寫入 HTTP 回應輸出資料流。

public:
 void Write(cli::array <char> ^ buffer, int index, int count);
public void Write (char[] buffer, int index, int count);
member this.Write : char[] * int * int -> unit
Public Sub Write (buffer As Char(), index As Integer, count As Integer)

參數

buffer
Char[]

要寫入的字元陣列。

index
Int32

開始寫入之字元陣列中的位置。

count
Int32

要寫入的字元數,從 index 開始。

範例

下列範例會使用 Write 方法建立一系列常數,這些常數會寫入至 ASP.NET 網頁。 程式碼會呼叫這個版本的 Write 方法,以將個別字元常數寫入頁面。

    <%

        // 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>

    %>
      <%
         Dim charArray As Char() = {"H"c, "e"c, "l"c, "l"c, "o"c, ","c, " "c, _
                                 "w"c, "o"c, "r"c, "l"c, "d"c}
         ' Write a character array to the client.
         Response.Write(charArray, 0, charArray.Length)

         ' Write a single character.
         Response.Write(";"c)

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

適用於