HttpResponse.Write 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將資訊寫入 HTTP 回應輸出串流。
多載
| 名稱 | Description |
|---|---|
| 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(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>")
備註
動態產生的 HTML 頁面若在收到 Web 用戶端輸入時或回傳給用戶端時未被驗證,可能會帶來安全風險。 惡意腳本嵌入提交給網站的輸入中,之後再寫回給客戶端,可能會看起來像是來自可信來源。 這種安全風險稱為跨站腳本攻擊。 當客戶端收到的資料從你的網站傳送到客戶端瀏覽器時,你應該始終驗證。
此外,當你將收到的輸入資料寫成 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>
%>