Programming Microsoft ASP.NET 4 - Dino Esposito [338]
Table 16-8. Methods of the Server Object
Method
Description
ClearError
Clears the last exception that was thrown for the request.
CreateObject
Creates an instance of the specified COM object.
CreateObjectFromClsid
Creates an instance of the COM object identified by the specified CLSID. The class identifier is expressed as a string.
Execute
Passes control to the specified page for execution. The child page executes like a subroutine. The output can be retained in a writer object or automatically flushed in the parent response buffer.
GetLastError
Returns the last exception that was thrown.
HtmlDecode
Decodes a string that has been encoded to eliminate invalid HTML characters. For example, it translates < into <.
HtmlEncode
Encodes a string to be displayed in a browser. For example, it encodes < into <.
MapPath
Returns the physical path that corresponds to the specified virtual path on the Web server.
Transfer
Works as a kind of server-side redirect. It terminates the execution of the current page and passes control to the specified page. Unlike Execute, control is not passed back to the caller page.
UrlDecode
Decodes a string encoded for HTTP transmission to the server in a URL. The decoded string can be returned as a string or output to a writer.
UrlEncode
Encodes a string for HTTP transmission to a client in a URL. The encoded string can be returned as a string or output to a writer.
UrlPathEncode
Encodes only the path portion of a URL string, and returns the encoded string. This method leaves the query string content intact.
UrlTokenDecode
Converts a URL string token, which encodes binary data as base 64 digits, to its equivalent byte array representation.
UrlTokenEncode
Encodes a byte array into its equivalent string representation using base 64 digits, which is usable for transmission on the URL.
HTML and URL encoding are ways of encoding characters to ensure that the transmitted text is not misunderstood by the receiving browser. HTML encoding, in particular, replaces <, >, &, and quotes with equivalent HTML entities such as <, >, &, and ". It also encodes blanks, punctuation characters, and in general, all characters not allowed in an HTML stream. On the other hand, URL encoding is aimed at fixing the text transmitted in URL strings. In URL encoding, the same critical characters are replaced with different character entities than in HTML encoding.
Embedding Another Page’s Results
The Execute method allows you to consider an external page as a subroutine. When the execution flow reaches the Server.Execute call, control is passed to the specified page. The execution of the current page is suspended, and the external page is spawned. The response text generated by the child execution is captured and processed according to the particular overload of Execute that has been used. Table 16-9 lists the overloads of the Execute method.
Table 16-9. Overloads of the Execute Method
Overload
Description
Execute(string);
You pass the URL of the page, and the response text is automatically embedded in the main page.
Execute(string, TextWriter);
The response text is accumulated in the specified text writer.
Execute(string, bool);
The same description as for previous item, except that you can choose whether to preserve the QueryString and Form collections. True is the default setting.
Execute(IHttpHandler, TextWriter, bool);
You indicate the HTTP handler to transfer the current request to. The response is captured by the text writer.
Execute(string, TextWriter, bool);
The response text is captured by the specified text writer, and the QueryString and Form collections are either preserved or not preserved, as specified.
Note that if a TextWriter object is specified, the response text of the child execution is accumulated into the writer object so that the main page output can be used later at