We have looked at the page life cycle and how a page can contain different controls. The page itself is instantiated as a control object. All web forms are basically instances of ASP.NET page classes. The page class has the following extremely useful properties, which correspond to the internal objects:
session
app
cache
ask
response
server
user
track
We will discuss each object at the appropriate time. In this tutorial we will explore the Server object, Request object and Response object.
The server object in ASP.NET is an instance of the System.Web.HttpServerUtility class. The HttpServerUtility class provides a large number of properties and methods to perform different tasks.
The methods and properties of the HttpServerUtility class are exposed through the internal server objects provided by ASP.NET.
The following table provides a series of properties of the HttpServerUtility class.
property | describe |
---|---|
MachineName | Server computer name |
ScriptTimeOut | Gets and sets the value of the request timeout in seconds |
The following table provides some important methods:
method | describe |
---|---|
CreateObject(String) | Creates an instance of the COM object, verified by its ProgID. |
CreateObject(Type) | Creates an instance of a COM object, verified by its Type. |
Equals(Object) | Determine whether a specific object is consistent with an existing object. |
Execute(String) | Execute the virtual path specified by the processing application in the context of the current request. |
Execute(String, Boolean) | Execute the virtual path specified by the handler in the context of the current request, specifying whether to clear the QueryString and forms collection. |
GetLastError | Return the previous exception. |
GetType | Gets the type of an existing instance. |
HtmlEncode | Convert an ordinary string into a legal HTML string. |
HtmlDecode | Convert an Html string into an ordinary string. |
ToString | Returns a string representing the current object. |
Transfer(String) | For the current request, terminates the execution of the current page and starts execution of a new page by specifying the URL path of the page. |
UrlDecode | Convert a URL string into a normal string. |
UrlEncodeToken | Same as UrlEncode, but contains Base64-encoded data in a byte array. |
UrlDecodeToken | Works the same as UrlDecode, but contains Base64-encoded data in a byte array. |
MapPath | Returns the physical path corresponding to the file path on the specified virtual server. |
Transfer | Transfer execution on the current application to another web page. |
The request object is an instance of the System.Web.HttpRequest class. It represents the values and attributes of the HTTP request that enable the page to be loaded into the browser.
The information presented by this object is encapsulated in a higher level abstraction (the web control model). However, this object can help inspect some information, such as client browsers and cookies.
The following table provides some noteworthy properties of the request object:
property | describe |
---|---|
AcceptTypes | Gets a string array of MIME accepted types supported by the user. |
ApplicationPath | Get the real application root path of the ASP.NET application on the server. |
Browser | Gets or sets information about the capabilities of the requesting user's browser. |
ContentEncoding | Gets or sets the character set entity. |
ContentLength | Specifies the length in bytes of content sent by the client. |
ContentType | Gets or sets the MIME content type of the incoming request. |
Cookies | Get the set of cookies sent by the client. |
FilePath | Get the real path of the current request. |
Files | Gets a collection of client-uploaded files in multipart MIME format. |
Form | Gets a collection of form variables. |
Headers | Gets a collection of HTTP headers. |
HttpMethod | Get the HTTP data transfer method used by the user (such as GET, POST, or HEAD) |
InputStream | Get the entity content of the incoming HTTP. |
IsSecureConnection | Gets a value indicating whether the HTTP connection uses secure sockets (that is, HTTPS). |
QueryString | Gets a collection of HTTP query string variables. |
RawUrl | Get the original URL of the current request. |
RequestType | Gets or sets the HTTP data transfer method (GET or POST) used by the user. |
ServerVariables | Gets a collection of web server variables. |
TotalBytes | Get the number of bytes of the existing input stream. |
Url | Gets information about an existing requested URL. |
UrlReferrer | Gets URL information about the client's previous request linked to an existing URL. |
UserAgent | Gets the client browser's original user-agent string. |
UserHostAddress | Get the IP host address of the remote client. |
UserHostName | Gets the DNS name of the remote client. |
UserLanguages | Gets a sorted string array of client language preferences. |
The following table provides some important methods:
method | describe |
---|---|
BinaryRead | Performs a binary reading of the specified number of bytes from the current input stream. |
Equals(Object) | Determines whether the specified object is equivalent to an existing object. (inherited from object) |
GetType | Gets the type of an existing instance. |
MapImageCoordinates | Draws the incoming image field form parameters to the appropriate x- and y-coordinate values. |
MapPath(String) | Draws the specified real path into a physical path. |
SaveAs | Stored on disk as an HTTP request. |
ToString | Returns a string representing the existing object. |
ValidateInput | Causes validation to occur by accessing the collection of Cookies, Form, and QueryString properties. |
The response object represents the server's response to the user's request. It is an instance of the System.Web.HttpResponse class.
In ASP.NET, response objects do not play any important role in sending HTML text to the user because server-side controls have nested, object-oriented methods for rendering themselves.
However, the HttpResponse object provides some important functionality, such as cookie characteristics and the Redirect() method. The Response.Redirect() method allows the user to be transferred to another page, either within the application or outside the application. It requires a round trip process.
The following table provides some noteworthy properties of response objects:
property | describe |
---|---|
Buffer | Gets or sets a value indicating whether output is buffered and sent after the complete responder has finished. |
BufferOutput | Gets or sets a value that indicates whether output is buffered and sent after the full page has finished. |
Charset | Gets or sets the HTTP character set of the output stream. |
ContentEncoding | Gets or sets the HTTP character set of the output stream. |
ContentType | Gets or sets the HTTP MIME type of the output stream. |
Cookies | Get the corresponding cookie collection. |
Expires | Gets or sets the number of minutes before cached pages on a browser expire. |
ExpiresAbsolute | Gets or sets the absolute date and time at which cached information is removed from the cache. |
HeaderEncoding | Gets or sets an encoding object representing the encoding of the existing header output stream. |
Headers | Gets a collection of response headers. |
IsClientConnected | Gets a value indicating whether the user is still connected to the server. |
Output | Causes the output text to be output to the HTTP response stream. |
OutputStream | Enables binary output to the HTTP content body of the output. |
RedirectLocation | Gets or sets the value of the Http header position. |
Status | Set the status bar and return it to the client. |
StatusCode | Gets or sets the HTTP output status code returned to the client. |
StatusDescription | Gets or sets the HTTP output status string returned to the client. |
SubStatusCode | Gets or sets a value that limits the response status code. |
SuppressContent | Gets or sets a value indicating whether to send HTTP content to the client. |
The following table provides some important methods:
method | describe |
---|---|
AddHeader | Adds an HTTP header to the output stream. AddHeader is provided for compatibility with earlier versions of ASP. |
AppendCookie | The infrastructure adds an HTTP cookie to the internal cookie collection. |
AppendHeader | Adds an HTTP header to the output stream. |
AppendToLog | Add custom log information to the Internet Information Services (IIS) log file. |
BinaryWrite | Writes a string of binary characters to the HTTP output stream. |
ClearContent | Clears all output from the buffered stream. |
Close | Close the client socket. |
End | Sends all existing buffered output to the client, stops page execution, and raises the EndRequest event. |
Equals(Object) | Determines whether the specified object is equivalent to an existing object. |
Flush | Send all existing buffered output to the client. |
GetType | Gets the type of an existing instance. |
Pics | Appends an HTTP PICS-Label header to the output stream. |
Redirect(String) | Redirect the request to a new URL and specify the new URL. |
Redirect(String, Boolean) | Redirect the client to a new URL. Specifies the new URL and whether the existing page should be terminated. |
SetCookie | Update existing cookies in the cookie collection. |
ToString | Returns a string representing the existing object |
TransmitFile(String) | Writes the specified file directly to an HTTP response output stream without buffering in memory. |
Write(Char) | Writes a character to an HTTP response output stream. |
Write(Object) | Writes an object into an HTTP response stream. |
Write(String) | Writes a string to an HTTP response output stream. |
WriteFile(String) | Writes the contents of the specified file directly to an HTTP response output stream, as a file block. |
WriteFile(String, Boolean) | Writes the contents of the specified file directly to an HTTP response output stream, as a memory block. |
The following simple example has a text box control that allows the user to enter a name, a button that sends information to the server, and a label control that displays the URL of the client computer.
Content files:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="server_side._Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> Enter your name: <br /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /> <br /> <asp:Label ID="Label1" runat="server"/> </div> </form> </body></html>
Button1_Click code after clicking:
protected void Button1_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(TextBox1.Text)) { // Access the HttpServerUtility methods through // the intrinsic Server object. Label1.Text = "Welcome, " + Server.HtmlEncode(TextBox1.Text) + ". <br/> The url is " + Server.UrlEncode(Request.Url.ToString()) }}
Run the page to see the following results: