HttpResponse.Filter 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定一個包裝過濾器物件,用於在傳輸前修改 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 物件並設定 Filter 該物件的 Stream 屬性時,所有 HTTP Write 輸出都會通過這個過濾器。