The HttpResponse class is used to send information
to the client's browser, including HTML content,
HTML headers, and customized cookies. Its name derives from the fact
that it is used to "respond" to an
HTTP request.
The Redirect( ) method of the
HttpResponse class provides the easiest way to
programmatically send the user to another web page. You supply the
name of the HTML or ASPX file as an argument (e.g.,
Response.Redirect ("newpage.aspx");). As long as
the file is in the same directory as the current page, you
don't need to provide a full URL (like
http://www.mysite/myapplication/newpage.aspx),
although you can use a relative path or fully-qualified URL. Other
ways to transfer a user between pages in an ASP.NET program include
the HttpServerUtility.Transfer( ) method and the
System.Web.UI.WebControls.HyperLink web control.
The Cookies property of the
HttpResponse class provides a reference to the
application's
HttpCookieCollection, which can send custom
cookies to the client. The Cache property provides
a reference to the application's
HttpCachePolicy settings. Both classes are
described separately. These properties, along with the
Redirect( ) method, are the most commonly used
members of HttpResponse.
In traditional ASP development, the Write( )
method was often used to append HTML to a web page (e.g.,
Reponse.Write "<h1>Hello
World</h1>";). ASP.NET programs will rarely use this
method because it is much easier to handle dynamic content by
changing the properties of full-featured web controls on Web Forms.
Similarly, the BinaryWrite( ) method, which allows
you to write binary information into the HTTP text stream by
supplying a byte array, or the WriteFile( )
method, which allows you to write the content from a named text file
into the output stream, are rarely used.
The BufferOutput property is a Boolean value that
determines whether or not the HTTP output is buffered. It is sent to
the client only when it is fully rendered and all code has executed.
The default is True. The
HttpResponse class also provides low-level control
over the management of the output buffer, with the Clear(
), Flush( ), and End(
) methods. You can also use the AppendToLog(
) method to write a string of information to the IIS log
file on the web server. This method should not be used for debugging,
as better options are provided by the TraceContext
class.
public sealed class HttpResponse {
// Public Constructors
public HttpResponse(System.IO.TextWriter writer);
// Public Instance Properties
public bool Buffer{set; get; }
public bool BufferOutput{set; get; }
public HttpCachePolicy Cache{get; }
public string CacheControl{set; get; }
public string Charset{set; get; }
public Encoding ContentEncoding{set; get; }
public string ContentType{set; get; }
public HttpCookieCollection Cookies{get; }
public int Expires{set; get; }
public DateTime ExpiresAbsolute{set; get; }
public Stream Filter{set; get; }
public bool IsClientConnected{get; }
public TextWriter Output{get; }
public Stream OutputStream{get; }
public string RedirectLocation{set; get; }
public string Status{set; get; }
public int StatusCode{set; get; }
public string StatusDescription{set; get; }
public bool SuppressContent{set; get; }
// Public Static Methods
public static void RemoveOutputCacheItem(string path);
// Public Instance Methods
public void AddCacheItemDependencies(System.Collections.ArrayList cacheKeys);
public void AddCacheItemDependency(string cacheKey);
public void AddFileDependencies(System.Collections.ArrayList filenames);
public void AddFileDependency(string filename);
public void AddHeader(string name, string value);
public void AppendCookie(HttpCookie cookie);
public void AppendHeader(string name, string value);
public void AppendToLog(string param);
public string ApplyAppPathModifier(string virtualPath);
public void BinaryWrite(byte[ ] buffer);
public void Clear( );
public void ClearContent( );
public void ClearHeaders( );
public void Close( );
public void End( );
public void Flush( );
public void Pics(string value);
public void Redirect(string url);
public void Redirect(string url, bool endResponse);
public void SetCookie(HttpCookie cookie);
public void Write(char ch);
public void Write(char[ ] buffer, int index, int count);
public void Write(object obj);
public void Write(string s);
public void WriteFile(IntPtr fileHandle, long offset, long size);
public void WriteFile(string filename);
public void WriteFile(string filename, bool readIntoMemory);
public void WriteFile(string filename, long offset, long size);
}