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 通過篩選。