The Server Object ASP.NET Objects: Interacting with the Framework

291 When this format is used, you can use any of the controls within the referenced namespace on the web page by including a tag of this form: tagprefix_name:class_name runat=server The second form of the Register directive registers a single control that resides in a source file. It looks like this: Register TagPrefix=tagprefix_name TagName=tagname Src=path When this format is used, you can use the specific control found in the source file on the web page by including a tag of this form: tagprefix_name:tagname runat=server In either form, you can set properties of the control object by specifying the properties as attributes of the tag, like this: tagprefix_name:tagname MyPropertyName=some value runat=server See Sect ion 6.11 later in this chapter for additional examples. The attributes of the Register directive are: TagPrefix A name that represents the namespace being referenced TagName A name that represents the class being referenced Namespace The namespace to reference Src The source file containing the control

6.6 ASP.NET Objects: Interacting with the Framework

The ASP.NET framework exposes functionality to web applications through a number of so-called intrinsic objects. These objects are available through the Server, Application, Session, Cache, Request, and Response properties of the Page object. This section gives an overview of these objects.

6.6.1 The Server Object

The Server object is an instance of the HttpServerUtility class defined in the System.Web namespace. This object provides information and services related to the web server. The HttpServerUtility class has two properties: 292 MachineName Gets the name of the machine on which the application is running. The type is String. ScriptTimeout Gets or sets the web-request timeout in seconds. The type is Integer. The default is 90 seconds. Some of the commonly used methods are: ClearError Clears the last exception that occurred see also GetLastError in this list. The syntax is: Public Sub ClearError Execute Executes a request to another page. After the other page is processed, processing of the current page continues. The Execute method has two overloads. The first is: Public Overloads Sub ExecuteByVal path As String The path parameter specifies the path of the web page file to be executed. With this form of the Execute method, the output of the web page is intermingled with the output of the calling page. The second overload is: Public Overloads Sub ExecuteByVal path As String, _ ByVal writer As System.IO.TextWriter Again, the path parameter specifies the path of the web page file to be executed. The writer parameter specifies an object of type TextWriter defined in the System.IO namespace that is to receive the output from the executed web page. The TextWriter class is not documented in this book. GetLastError Gets the last exception that was thrown see also ClearError in this list. The syntax is: Public Function GetLastError As System.Exception HtmlEncode and HtmlDecode Encode and decode strings, respectively, that might contain HTML characters. Each method has two overrides: one that returns the encoded or decoded string as a String and one that writes the encoded or decoded string into an object of type TextWriter defined in the System.IO namespace. The TextWriter class is not documented in this book. The syntax for these methods is: Public Overloads Function HtmlEncodeByVal s As String As String The s parameter is the string to encode. The return value of the function is the encoded string. Public Overloads Sub HtmlEncodeByVal s As String, _ ByVal output As System.IO.TextWriter 293 The s parameter is the string to encode. The encoded string is written to the TextWriter object passed in the output parameter. Public Overloads Function HtmlDecodeByVal s As String As String The s parameter is the string to decode. The return value of the function is the decoded string. Public Overloads Sub HtmlDecodeByVal s As String, _ ByVal output As System.IO.TextWriter The s parameter is the string to decode. The decoded string is written to the TextWriter object passed in the output parameter. MapPath Returns the physical path that corresponds to the given virtual path. The syntax is: Public Function MapPathByVal path As String As String Transfer Stops execution of the current page and begins execution of the specified page. This method differs from the Redirect method of the Response object. The Redirect method directs the browser to request a new page, whereas the Transfer method simply starts executing a new page. The Transfer method has two overrides. The first is: Public Overloads Sub TransferByVal path As String The path parameter specifies the virtual path of the new page. The second override is: Public Overloads Sub TransferByVal path As String, _ ByVal preserveForm As Boolean UrlEncode and UrlDecode Encode and decode strings, respectively, so that they can be used within URLs. Each method has two overrides: one that returns the encoded or decoded string as a String and one that writes the encoded or decoded string into an object of type TextWriter defined in the System.IO namespace. The TextWriter class is not documented in this book. The syntax for these methods is: Public Overloads Function UrlEncodeByVal s As String As String The s parameter is the string to encode. The return value of the function is the encoded string. Public Overloads Sub UrlEncodeByVal s As String, _ ByVal output As System.IO.TextWriter The s parameter is the string to encode. The encoded string is written to the TextWriter object passed in the output parameter. Public Overloads Function UrlDecodeByVal s As String As String The s parameter is the string to decode. The return value of the function is the decoded string. Public Overloads Sub UrlDecodeByVal s As String, _ ByVal output As System.IO.TextWriter 294 The s parameter is the string to decode. The decoded string is written to the TextWriter object passed in the output parameter.

6.6.2 The Application Object