HttpResponse.Filter 属性

定义

获取或设置一个包装筛选器对象,该对象用于在传输之前修改 HTTP 实体主体。

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 对象。

例外

不允许对实体进行筛选。

示例

下面的示例是一个 ASP.NET 页,该页将 属性设置为 Filter 类的新实例 UpperCaseFilter ,该类是一个自定义 Stream 类,用于将传递该类的所有文本转换为大写。 有关请求的信息将保存到文本文件中,然后 Filter 设置 属性。 响应筛选器就位后,代码调用 MapPath 方法以获取名为 TestFile.txt 的文本文件的绝对路径,该文件用作响应内容的源。 然后,代码创建一个新的 StreamReader 对象以从头到尾读取文本文件,然后调用 Write 方法以在页面上显示文件的内容。

<%@ 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 对象并将 属性设置为 FilterStream 对象时,发送 Write 的所有 HTTP 输出都会通过筛选器传递。

适用于