HttpResponse.Filter 屬性

定義

取得或設定用來在傳輸之前修改 HTTP 實體主體的包裝篩選物件。Gets or sets a wrapping filter object that is used to modify the HTTP entity body before transmission.

public:
 property System::IO::Stream ^ Filter { System::IO::Stream ^ get(); void set(System::IO::Stream ^ value); };
public System.IO.Stream Filter { get; set; }
member this.Filter : System.IO.Stream with get, set
Public Property Filter As Stream

屬性值

Stream 物件,做為輸出篩選條件。The Stream object that acts as the output filter.

例外狀況

不允許以實體篩選。Filtering is not allowed with the entity.

範例

下列範例是一個 ASP.NET 網頁,它會將 Filter 屬性設定為 UpperCaseFilter 類別的新實例,這是一個自訂的 Stream 類別,可將通過它的所有文字轉換成大寫。The following example is an ASP.NET page that sets the Filter property to a new instance of the UpperCaseFilter class, a custom Stream class that converts all text that passes through it to uppercase. 要求的相關資訊會儲存到文字檔中,然後設定 Filter 屬性。The information about the request is saved to a text file, and then the Filter property is set. 回應篩選器就緒之後,程式碼會呼叫 MapPath 方法,以取得名為 TestFile.txt 之文字檔的絕對路徑,做為回應內容的來源。After the response filter is in place, the code calls the MapPath method to get the absolute path to a text file named TestFile.txt that serves as the source for the content of the response. 然後,程式碼會建立新的 StreamReader 物件,以便從開頭到結尾讀取文字檔,然後再呼叫 Write 方法,在頁面上顯示該檔案的內容。The code then creates a new StreamReader object to read the text file from beginning to end, and then calls the Write method to display the content of the file on the page.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<%@ import Namespace="Samples.AspNet.CS.Controls"  %>
<!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)
    {

      // Filter the text to be rendered as all uppercase.
      Response.Filter = new UpperCaseFilterStream(Response.Filter);

      // Convert a virtual path to a fully qualified physical path.
      string fullpath = Request.MapPath("~\\TestFile.txt");

      try
      {
        // Read the contents of the file using a StreamReader.
        using (StreamReader sr = new StreamReader(fullpath))
        while (sr.Peek() >= 0)
        {
          Response.Write((char)sr.Read());
        }
        Message.Text = "Reading the file was successful.";
        
      }
      catch (Exception ex)
      {
        Message.Text = "The process failed.";
      }    
     }

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

      <asp:Label id="Message" 
                 runat="server"/>

    </form>
  </body>
</html>
<%@ Page Language="VB" Debug="true"%>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="Samples.AspNet.VB.Controls" %>
<!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(ByVal sender As Object, ByVal e As EventArgs)
      
    ' Filter the text to be rendered as all uppercase.
    Response.Filter = New UpperCaseFilterStream(Response.Filter)
    
    ' Convert a virtual path to a fully qualified physical path.
    Dim fullpath As String = Request.MapPath("~\\TestFile.txt")
    
    Try
      
      Dim sr As StreamReader = New StreamReader(fullpath)
      
      Do While sr.Peek() >= 0
        Response.Write(Convert.ToChar(sr.Read()))
      Loop
      sr.Close()
      Message.Text = "Reading the file was successful."
      
    Catch ex As Exception
      
      Message.Text = "The process failed."

    End Try

    
  End Sub

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

      <asp:Label id="Message" 
                 runat="server"/>

    </form>
  </body>
</html>

備註

當您建立 Stream 物件,並將 Filter 屬性設定為 Stream 物件時,Write 傳送的所有 HTTP 輸出都會通過篩選。When you create a Stream object and set the Filter property to the Stream object, all HTTP output sent by Write passes through the filter.

適用於