ASP. NET 3.5 core programming learning notes (29): httpserverutility class

Source: Internet
Author: User

The server object function in ASP is implemented by the httpserverutility class in ASP. NET. This type of instance is created when as. Net starts processing the request and then stored in the request context. Httpserverutility has a large number of auxiliary methods, which are exposed to modules or handlers (including global. asax, pages, and Web Services) through the server attribute of the httpcontext object ).

Attributes of the httpserverutility class

This class provides two attributes: machinename and scripttimeout. The machinename attribute returns the name of the computer on the current server. The scripttimeout attribute gets or sets the maximum time used to process requests (in seconds ). This attribute accepts an integer. The default value is 90. If the page runs in the DEBUG = true state, this attribute is set to an infinitely large value.

In the dynamically created page class, the scripttimeout attribute is automatically displayed in its constructor.

Methods of the httpserverutility class

The following table lists all methods of the httpserverutility class:

HTML and URL encoding are intended to ensure that the transmitted text is not recognized by the browser that receives it. Specifically, HTML encoding replaces <,>, &, and quotation marks with equivalent HTML entities, such as & lt;, & gt;, & amp;, and & quot ;. This process also encodes white space, punctuation marks, and other characters not allowed in the HTML stream. URL encoding is used to modify the text transmitted through a URL string. During URL encoding, special characters are also replaced by other instances, but these entities are different from HTML encoding.

Since ASP. NET 2.0, this class adds two static methods for token encoding and decoding respectively. The urltokenencode method accepts a byte array containing base64 data, which can be converted into URL-encoded tags. Urltokendecode can execute the reverse conversion process.

Embedded results on other pages

The execute method allows us to execute external pages as subprograms. After the server. Execute Command is executed, the control is passed to the specified page. The execution of the current page will be suspended and the external page will be activated. The response text generated on the Child page is captured and processed according to the execute overload method used. The following table lists the overloaded methods of the execute method:

If textwriter is specified, the response text of the subpage is cached in the writer object, so that you can freely manage the output of the home page. The following example shows the effect: the text generated on the home page is in bold, and the output on the Child page is in the normal font, Which is clipped between the two rows of output on the home page.

void Page_Load(object sender, EventArgs e)
{
StringBuilder builder = new StringBuilder();
builder.Append("<b>Response generated before Execute is called</b>
StringWriter writer = new StringWriter();
Server.Execute("child.aspx", writer);
builder.Append(writer.ToString());

builder.Append("
Label1.Text = builder.ToString();
}

The internal implementation of the execute method is quite interesting. The home page and subpages are run by the same httpapplication object, as if they are processing the same request. Execute is a context switch. First, this method gets an HTTP handler from the application factory to serve the new request. The original handler of the master request will be cached, and the new handler will replace it. The child page inherits the context of its parent page. After the sub-page is executed, any changes to the session and application will immediately take effect on the home page.

The entire operation is extremely efficient because you do not need to create a new object to process the request for switching. When the sub-page returns, the original processing program is restored. The execution on the homepage will continue from the paused position, but the context used is inherited from the subpage.

ASP. NET directly calls the handler indicated by the execute method without applying any authentication and empty authorization logic. If your security policy requires the client to have the permission to access this resource, the application should be re-authorized. We can use response. Redirect instead of execute to force re-authorization. If redirect is called, the browser sends a new request to the system, which is usually authenticated and authorized by IIS and ASP. NET. Another method is to define the role and check the user role before the application calls the execute method to verify whether the user has the permission to call the subpage.

Server redirection

The difference between the transfer method and the execute method is that the transfer method terminates the execution of the master page after the specified page is executed. If the execution result of the new page is the page of the initial request. The transfer method has the following three overload methods:

public void Transfer(string);
public void Transfer(string, bool);
public void Transfer(IHttpHandler, bool);

String parameters are used to specify the target URL. The bool type parameter is used to specify the processing method of querystring and form. If it is set to true, the two sets will be retained; otherwise, both sets will be cleared. The ihttphandler type parameter can directly specify the HTTP handler to be called.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.