asp.net.ph

Response.Filter Property

System.Web Namespace   HttpResponse Class


Sets or retrieves a wrapping filter object used to modify the HTTP entity body before transmission.

[ VB ]
Public Property Filter As Stream

[ C# ]
public Stream Filter {get; set;}

[ C++ ]
public: __property Stream* get_Filter ( );
public: __property void set_Filter ( Stream* );

[ JScript ]
public function get Filter ( ) : Stream;
public function set Filter ( Stream );

Property Value

The Stream object that acts as the output filter.

Exceptions


Exception Type Condition
HttpException Occurs when Filtering is not allowed with the entity.

Remarks

When you create a Stream object and set the Response.Filter property to the Stream object, all HTTP output sent by Response.Write passes through the filter.

Example

The following example creates a new Stream object named UpperCaseFilter in the namespace ResponseFilters.

The C# or Visual Basic file is compiled into a file named ResponseFilters.dll.

An ASP.NET page sets the Response.Filter property to the new object in order to change all text sent by the page through Response.Write to Upper case.

using System;
using System.IO;

namespace ResponseFilters
{

   public class UpperCaseFilter : Stream
   // This filter changes all characters passed through it to uppercase.
   {
      private Stream _sink;
     private long _position;

      public UpperCaseFilter ( Stream sink ) 
      {
          _sink = sink;
    }

      // The following members of Stream must be overriden.
      public override bool CanRead
      {
         get { return true; }
     }

      public override bool CanSeek
      {
         get { return true; }
     }

      public override bool CanWrite
      {
         get { return true; }
     }

      public override long Length
      {
         get { return 0; }
     }

      public override long Position
      {
         get { return _position; }
         set { _position = value; }
     }

      public override long Seek ( long offset, System.IO.SeekOrigin direction ) 
      {
         return _sink.Seek ( offset, direction );
    }

      public override void SetLength ( long length ) 
      {
         _sink.SetLength ( length );
    }

      public override void Close ( ) 
      {
         _sink.Close ( );
    }

      public override void Flush ( ) 
      {
         _sink.Flush ( );
    }

      public override int Read ( byte [ ] buffer, int offset, int count ) 
      {
         return _sink.Read ( buffer, offset, count );
    }

      // The Write method actually does the filtering.
      public override void Write ( byte [ ] buffer, int offset, int count ) 
      {
         byte [ ] data = new byte [ count ];
        Buffer.BlockCopy ( buffer, offset, data, 0, count );

         for ( int i = 0; i < count; i++ ) 
         // Change lowercase chars to uppercase.
         {
           If ( data [ i ] >= 'a' && data [ i ] <= 'z' ) 
               data [ i ] -= ( 'a'-'A' );
       }

         _sink.Write ( data, 0, count );
      
     }

      // Main is included for code example testing.
      // It is not required when compiling to a dll.
      public static void Main ( ) 
      {
     }
   }
}


/*
______________________________________________________________

The following .aspx page uses the above filter to modify HTTP output.

<%@ Page Language="C#" %>
<%@ assembly name = "ResponseFilters" %>

<script runat=server>
  protected void Page_Load ( object src, EventArgs ev ) 
   {
      Response.Filter = new ResponseFilters.UpperCaseFilter ( Response.Filter );
  }
</script>

<HTML>
  <BODY>
  <%    
   Response.Write ( "This text is changed to upper case by UpperCaseFilter.<br>" );
  %>
  </BODY>
</HTML>
*/

   . . . 
  C# VB

See Also

HttpResponse Members Skip Navigation Links




Home
Suggested Reading


Previous page Back to top Next page

© 2000-2010 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support asp.net.ph