Language

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 オブジェクト。

例外

エンティティでのフィルター処理は許可されません。

例

次の例は、 Filter プロパティを UpperCaseFilter クラスの新しいインスタンスに設定する ASP.NET ページです。これは、それを通過するすべてのテキストを大文字に変換するカスタム 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 オブジェクトを作成し、Filter プロパティを Stream オブジェクトに設定すると、Writeによって送信されるすべての HTTP 出力がフィルターを通過します。

適用対象