共用方式為


HttpResponse 類別

定義

封裝來自 ASP.NET 操作的 HTTP 回應資訊。

public ref class HttpResponse sealed
public sealed class HttpResponse
type HttpResponse = class
Public NotInheritable Class HttpResponse
繼承
HttpResponse

範例

以下範例在請求頁面時繪製三個重疊的矩形。 程式碼一開始會將屬性設定 ContentType 為 image/jpeg,這樣整頁就能以 JPEG 影像呈現。 程式碼接著呼叫該 Clear 方法,以確保此回應不會傳送多餘內容。 接著,程式碼將該屬性設 BufferOutput 為 true,確保頁面在送達請求客戶端前被完整處理。 接著會產生兩個用來繪製矩形的物件:a Bitmap 和 a Graphics 物件。 頁面中建立的變數作為座標,繪製矩形及出現在最大矩形內的字串。

當繪製三個矩形及其內的字串時,會 Bitmap 儲存到 Stream 與屬性 OutputStream 相關的物件,並設定為 JPEG。 程式碼呼叫 和 DisposeDispose 方法來釋放兩個繪圖物件所使用的資源。 最後,程式碼會呼叫 Flush 方法,將緩衝回應傳送給請求的客戶端。

備註

在程式碼中, HttpResponse 物件以關鍵字 Response來指稱。 例如,指 Response.Clear() 的是 方法 HttpResponse.Clear 。 該 Page 類別有一個名為 Response 的屬性,該屬性會暴露 的 HttpResponse當前實例。

<%@ Page Language="C#" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    private void Page_Load(object sender, EventArgs e)
    {
// <snippet2>
        // Set the page's content type to JPEG files
        // and clears all content output from the buffer stream.
        Response.ContentType = "image/jpeg";
        Response.Clear();
    
        // Buffer response so that page is sent
        // after processing is complete.
        Response.BufferOutput = true;
// </snippet2>
    
        // Create a font style.
        Font rectangleFont = new Font(
            "Arial", 10, FontStyle.Bold);
    
        // Create integer variables.
        int height = 100;
        int width = 200;
    
        // Create a random number generator and create
        // variable values based on it.
        Random r = new Random();
        int x = r.Next(75);
        int a = r.Next(155);
        int x1 = r.Next(100);
    
        // Create a bitmap and use it to create a
        // Graphics object.
        Bitmap bmp = new Bitmap(
            width, height, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(bmp);
    
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.Clear(Color.LightGray);
    
        // Use the Graphics object to draw three rectangles.
        g.DrawRectangle(Pens.White, 1, 1, width-3, height-3);
        g.DrawRectangle(Pens.Aquamarine, 2, 2, width-3, height-3);
        g.DrawRectangle(Pens.Black, 0, 0, width, height);
    
        // Use the Graphics object to write a string
        // on the rectangles.
        g.DrawString(
            "ASP.NET Samples", rectangleFont,
            SystemBrushes.WindowText, new PointF(10, 40));
    
        // Apply color to two of the rectangles.
        g.FillRectangle(
            new SolidBrush(
                Color.FromArgb(a, 255, 128, 255)),
            x, 20, 100, 50);
    
        g.FillRectangle(
            new LinearGradientBrush(
                new Point(x, 10),
                new Point(x1 + 75, 50 + 30),
                Color.FromArgb(128, 0, 0, 128),
                Color.FromArgb(255, 255, 255, 240)),
            x1, 50, 75, 30);

// <snippet3>    
        // Save the bitmap to the response stream and
        // convert it to JPEG format.
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
    
        // Release memory used by the Graphics object
        // and the bitmap.
        g.Dispose();
        bmp.Dispose();
    
        // Send the output to the client.
        Response.Flush();
// </snippet3>
    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

   Private Sub Page_Load(sender As Object, e As EventArgs)
' <snippet2>
      ' Set the page's content type to JPEG files
      ' and clears all content output from the buffer stream.
      Response.ContentType = "image/jpeg"
      Response.Clear()
      
      ' Buffer response so that page is sent
      ' after processing is complete.
      Response.BufferOutput = True
' </snippet2>
      
      ' Create a font style.
      Dim rectangleFont As New Font( _
          "Arial", 10, FontStyle.Bold)
      
      ' Create integer variables.
      Dim height As Integer = 100
      Dim width As Integer = 200
      
      ' Create a random number generator and create
      ' variable values based on it.
      Dim r As New Random()
      Dim x As Integer = r.Next(75)
      Dim a As Integer = r.Next(155)
      Dim x1 As Integer = r.Next(100)
      
      ' Create a bitmap and use it to create a
      ' Graphics object.
      Dim bmp As New Bitmap( _
          width, height, PixelFormat.Format24bppRgb)
      Dim g As Graphics = Graphics.FromImage(bmp)
      
      g.SmoothingMode = SmoothingMode.AntiAlias
      g.Clear(Color.LightGray)
      
      ' Use the Graphics object to draw three rectangles.
      g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3)
      g.DrawRectangle(Pens.Aquamarine, 2, 2, width - 3, height - 3)
      g.DrawRectangle(Pens.Black, 0, 0, width, height)
      
      ' Use the Graphics object to write a string
      ' on the rectangles.
      g.DrawString("ASP.NET Samples", rectangleFont, SystemBrushes.WindowText, New PointF(10, 40))
      
      ' Apply color to two of the rectangles.
      g.FillRectangle( _
          New SolidBrush( _
              Color.FromArgb(a, 255, 128, 255)), _
          x, 20, 100, 50)
      
      g.FillRectangle( _
          New LinearGradientBrush( _
              New Point(x, 10), _
              New Point(x1 + 75, 50 + 30), _
              Color.FromArgb(128, 0, 0, 128), _
              Color.FromArgb(255, 255, 255, 240)), _
          x1, 50, 75, 30)

' <snippet3>      
      ' Save the bitmap to the response stream and
      ' convert it to JPEG format.
      bmp.Save(Response.OutputStream, ImageFormat.Jpeg)
      
      ' Release memory used by the Graphics object
      ' and the bitmap.
      g.Dispose()
      bmp.Dispose()
      
      ' Send the output to the client.
      Response.Flush()
' </snippet3>
   End Sub 'Page_Load

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

備註

該類別的方法HttpResponse與性質透過 、 HttpContextUserControlPage和 類別的性質HttpApplication揭露Response

以下類別方法 HttpResponse 僅支援於後回傳情境,非同步後回傳情境不支援:

部分頁面更新是因為你使用 UpdatePanel 控制項來更新頁面的部分區域,而不是用貼文更新整個頁面。 更多資訊請參閱 UpdatePanel 控制概述Partial-Page 渲染概覽

建構函式

名稱 Description
HttpResponse(TextWriter)

初始化 HttpResponse 類別的新執行個體。

屬性

名稱 Description
Buffer

會接收或設定一個值,指示是否緩衝輸出並在完整回應處理完成後再傳送。

BufferOutput

取得或設定一個值,指示是否緩衝輸出並在完整頁面處理完成後傳送。

Cache

取得網頁的快取政策(例如有效期限、隱私設定及各種條款)。

CacheControl

取得或設定 Cache-Control 與列 HttpCacheability 舉值相符的 HTTP 標頭。

Charset

取得或設定輸出串流的 HTTP 字元集。

ClientDisconnectedToken

當客戶端斷線時,會觸發 CancellationToken 一個物件。

ContentEncoding

取得或設定輸出串流的 HTTP 字元集。

ContentType

取得或設定輸出串流的 HTTP MIME 類型。

Cookies

會收到回應的 cookie 集合。

Expires

讀取或設定瀏覽器快取頁面過期前的分鐘數。 如果使用者在頁面過期前返回同一頁面,則會顯示快取版本。 Expires 是為了與早期版本的 ASP 相容而提供。

ExpiresAbsolute

取得或設定從快取中移除快取資訊的絕對日期與時間。 ExpiresAbsolute 是為了與早期版本的 ASP 相容而提供。

Filter

取得或設定一個包裝過濾器物件,用於在傳輸前修改 HTTP 實體實體。

HeaderEncoding

取得或設定 Encoding 一個物件,代表當前標頭輸出串流的編碼。

Headers

會取得一組回應標頭。

HeadersWritten

會取得一個值,表示回應標頭是否已被寫入。

IsClientConnected

會取得一個值,表示用戶端是否仍連接伺服器。

IsRequestBeingRedirected

會獲得一個布林值,表示客戶端是否正在被轉移到新地點。

Output

允許將文字輸出至發出的 HTTP 回應串流。

OutputStream

啟用二進位輸出至發出的 HTTP 內容主體。

RedirectLocation

取得或設定 HTTP Location 標頭的值。

Status

設定 Status 回傳給客戶端的行。

StatusCode

取得或設定回傳給用戶端的輸出的 HTTP 狀態碼。

StatusDescription

取得或設定回傳給用戶端的 HTTP 狀態字串。

SubStatusCode

取得或設定一個值,以限定回應的狀態碼。

SupportsAsyncFlush

會取得一個值,表示該連線是否支援非同步沖洗操作。

SuppressContent

會取得或設定一個值,指示是否要將 HTTP 內容傳送給用戶端。

SuppressDefaultCacheControlHeader

取得或設定一個值,指示是否要抑制目前 HTTP 回應的預設 Cache Control: private 標頭。

SuppressFormsAuthenticationRedirect

取得或設定一個值,指定是否應該抑制表單認證重定向至登入頁面。

TrySkipIisCustomErrors

取得或設定一個值,指定是否停用 IIS 7.0 自訂錯誤。

方法

名稱 Description
AddCacheDependency(CacheDependency[])

將一組快取相依關聯到回應,以便當回應儲存在輸出快取中且指定的相依關係改變時,能夠使其失效。

AddCacheItemDependencies(ArrayList)

讓快取回應的有效性取決於快取中其他項目。

AddCacheItemDependencies(String[])

這使得快取項目的有效性依賴於快取中另一個項目。

AddCacheItemDependency(String)

這使得快取回應的有效性依賴於快取中的另一個項目。

AddFileDependencies(ArrayList)

將一組檔案名稱加入當前回應所依賴的檔案名稱集合中。

AddFileDependencies(String[])

將檔案名稱陣列加入當前回應所依賴的檔案名稱集合中。

AddFileDependency(String)

將單一檔案名稱加入當前回應所依賴的檔案名稱集合中。

AddHeader(String, String)

在輸出串流中加入 HTTP 標頭。 AddHeader(String, String) 是為了與早期版本的 ASP 相容而提供。

AddOnSendingHeaders(Action<HttpContext>)

會註冊一個回調,ASP.NET 執行時會在該請求發送回應標頭前立即呼叫該回調。

AppendCookie(HttpCookie)

將 HTTP cookie 加入內在 cookie 集合中。

AppendHeader(String, String)

在輸出串流中加入 HTTP 標頭。

AppendToLog(String)

新增自訂日誌資訊至網際網路資訊服務(IIS)日誌檔案。

ApplyAppPathModifier(String)

如果虛擬路徑使用 Cookieless 會話狀態,則會為虛擬路徑新增一個 ID,並回傳合併後的路徑。 若 Cookieless 未使用會話狀態,則 ApplyAppPathModifier(String) 回傳原始虛擬路徑。

BeginFlush(AsyncCallback, Object)

將目前緩衝的回應傳送給用戶端。

BinaryWrite(Byte[])

將一串二進位字元寫入 HTTP 輸出串流。

Clear()

清除緩衝區串流中的所有內容輸出。

ClearContent()

清除緩衝區串流中的所有內容輸出。

ClearHeaders()

清除緩衝區串流中的所有標頭。

Close()

關閉與客戶端的 socket 連線。

DisableKernelCache()

關閉目前回應的內核快取。

DisableUserCache()

此回應中關閉 IIS 使用者模式快取。

End()

將所有目前緩衝的輸出傳送給用戶端,停止頁面執行,並啟動 EndRequest 事件。

EndFlush(IAsyncResult)

完成非同步沖洗操作。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Flush()

將所有目前緩衝的輸出傳送給用戶端。

FlushAsync()

非同步地將目前緩衝中的所有輸出傳送到用戶端。

GetHashCode()

做為預設哈希函式。

(繼承來源 Object)
GetType()

取得目前實例的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
Pics(String)

在輸出串流中附加一個 HTTP PICS-Label 標頭。

PushPromise(String, String, NameValueCollection)

支援應用程式向 HTTP 2.0 用戶端發送推送承諾。 欲了解更多資訊,請參閱 HTTP/2 規範第 8.2 節:伺服器推送

PushPromise(String)

支援應用程式向 HTTP 2.0 用戶端發送推送承諾。 欲了解更多資訊,請參閱 HTTP/2 規範第 8.2 節:伺服器推送

Redirect(String, Boolean)

將客戶端重新導向到新的網址。 指定新的網址,以及當前頁面的執行是否應該終止。

Redirect(String)

將請求重新導向到新的網址並指定新的網址。

RedirectPermanent(String, Boolean)

執行從請求的 URL 永久導向到指定的 URL,並提供完成回應的選項。

RedirectPermanent(String)

執行從請求的 URL 永久重定向到指定的 URL。

RedirectToRoute(Object)

透過路由參數值將請求重新導向到新的 URL。

RedirectToRoute(RouteValueDictionary)

透過路由參數值將請求重新導向到新的 URL。

RedirectToRoute(String, Object)

透過路由參數值和路由名稱,將請求重新導向到新的 URL。

RedirectToRoute(String, RouteValueDictionary)

透過路由參數值和路由名稱,將請求重新導向到新的 URL。

RedirectToRoute(String)

透過路由名稱將請求重新導向到新的 URL。

RedirectToRoutePermanent(Object)

透過路由參數值,從請求的 URL 永久重定向到新 URL。

RedirectToRoutePermanent(RouteValueDictionary)

透過路由參數值,從請求的 URL 永久重定向到新 URL。

RedirectToRoutePermanent(String, Object)

透過使用路由參數值及對應新 URL 的路由名稱,執行從請求 URL 永久重定向到新 URL。

RedirectToRoutePermanent(String, RouteValueDictionary)

透過路由參數值與路由名稱,從請求的 URL 永久重定向到新 URL。

RedirectToRoutePermanent(String)

透過路由名稱,將請求的 URL 永久重定向到新的 URL。

RemoveOutputCacheItem(String, String)

使用指定的輸出快取提供者移除所有與指定路徑相關的輸出快取項目。

RemoveOutputCacheItem(String)

從快取中移除所有與預設輸出快取提供者相關聯的快取項目。 此方法是靜態的。

SetCookie(HttpCookie)

由於 HttpResponse.SetCookie 方法僅供內部使用,你不應該在程式碼中呼叫它。 相反地,你可以呼叫 HttpResponse.Cookies.Set 方法,如下範例所示。
更新 cookie 集合中現有的 cookie。

ToString()

傳回表示目前 物件的字串。

(繼承來源 Object)
TransmitFile(String, Int64, Int64)

將指定部分檔案直接寫入 HTTP 回應輸出串流,且不會在記憶體中緩衝。

TransmitFile(String)

將指定的檔案直接寫入 HTTP 回應輸出串流,且不會在記憶體中緩衝。

Write(Char)

將一個字元寫入 HTTP 回應的輸出串流。

Write(Char[], Int32, Int32)

將一個字元陣列寫入 HTTP 回應輸出串流。

Write(Object)

將 寫入 , Object 寫入 HTTP 回應串流。

Write(String)

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

WriteFile(IntPtr, Int64, Int64)

直接將指定的檔案寫入 HTTP 回應輸出串流。

WriteFile(String, Boolean)

將指定檔案的內容直接寫入 HTTP 回應輸出串流,作為記憶體區塊。

WriteFile(String, Int64, Int64)

直接將指定的檔案寫入 HTTP 回應輸出串流。

WriteFile(String)

將指定檔案的內容直接寫入 HTTP 回應輸出串流,作為檔案區塊。

WriteSubstitution(HttpResponseSubstitutionCallback)

允許在回應中插入回應替換區塊,從而動態產生指定的回應區域以回應快取。

適用於