HttpResponse.Filter Proprietà

Definizione

Ottiene o imposta un oggetto filtro di wrapping usato per modificare il corpo dell'entità HTTP prima della trasmissione.

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

Valore della proprietà

Oggetto Stream che funge da filtro di output.

Eccezioni

L'applicazione di filtri non è consentita con l'entità.

Esempio

L'esempio seguente è una pagina ASP.NET che imposta la proprietà su una nuova istanza della UpperCaseFilter classe, una classe personalizzata Stream che converte tutto il Filter testo che lo passa in maiuscolo. Le informazioni sulla richiesta vengono salvate in un file di testo e quindi la Filter proprietà viene impostata. Al termine del filtro di risposta, il codice chiama il metodo per ottenere il MapPath percorso assoluto di un file di testo denominato TestFile.txt che funge da origine per il contenuto della risposta. Il codice crea quindi un nuovo StreamReader oggetto per leggere il file di testo dall'inizio alla fine e quindi chiama il metodo per visualizzare il Write contenuto del file nella pagina.

<%@ 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>

Commenti

Quando si crea un Stream oggetto e si imposta la Filter proprietà sull'oggetto Stream , tutti gli output HTTP inviati Write passano attraverso il filtro.

Si applica a