Server object, HttpServerUtility class, getting server information

Source: Internet
Author: User
Tags httpcontext urlencode

in ASP. NET WebForm, the server object is an instance of the HttpServerUtility class. See:

  

in ASP. NET MVC, the server object is a Httpserverutilitybase object.

Take a look at the functionality provided by the HttpServerUtility class. By the way, a ashx page to show the role of the class.

First, the attribute

MachineName Gets the computer name of the server.

ScriptTimeout gets and sets the request time-out value in seconds.

        void ProcessRequest (HttpContext context)        {            context. Response.Write (context. Server.machinename);     // output KISSDODOG-PC get the computer name            context. Response.Write (context. Server.ScriptTimeout);   // output    

Second, the method

ClearError clears the previous exception.

CreateObject is overloaded. Creates a server instance of a COM object.

Createobjectfromclsid creates a server instance of a COM object, identified by the object's class identifier (CLSID).

Execute is overloaded. Executes the handler for the specified resource in the context of the current request, and then returns execution to the page that called it.

GetLastError returns the previous exception.

HtmlDecode is overloaded. Decodes a string that has been encoded to eliminate invalid HTML characters.

HtmlEncode is overloaded. Encodes the string to be displayed in the browser.

MapPath returns the physical file path corresponding to the specified virtual path on the WEB server. This method is often used. Converts a virtual path to a server physical path.

Transfer is overloaded. Terminates execution of the current page and begins execution of a new page for the current request.

TransferRequest is overloaded. Executes the specified URL asynchronously.

UrlDecode is overloaded. Decodes a string that encodes the HTTP transport and sends it to the server in the URL.

UrlEncode is overloaded. Encodes a string for reliable HTTP transmission from the WEB server to the client via a URL.

Urlpathencode URL-encodes the path portion of the URL string and returns the encoded string.

Urltokendecode decodes the URL string token to an equivalent byte array that uses a 64 binary number.

Urltokenencode encodes a byte array as an equivalent string representation of the base 64 encoding scheme, and base 64 is an encoding scheme that is suitable for transmitting data over a URL.

 context. Response.Write (context.     Server.MapPath ( "/"); // output C:\Users\Administrator\Desktop\WebApplication1 \webapplication1\ // " "); // "// output after encoding       

The effect is as follows:

  

We decode the HTML-encoded character and then output it to see the effect.

            "<div style=& #39; #39;> HTMLEncode Test </div> ";            Context. Response.Write (context. Server.htmldecode (str));  

The effect is as follows

  

From the effect, we see that after encoding the effect is changed back to be parsed by the browser.

Here's a look at the Excute method:

Test.ashx page:

        void ProcessRequest (HttpContext context)        {            context. Response.Write (" I am the first text.ashx to deal with the results"); Context. Response.Write ("<br/>"); Context. Server.Execute ("/default.aspx"); Context. Response.Write (" I am the second text.ashx processing result ");}          

Default.aspx page code:

        void Page_Load (object sender, EventArgs e)        {            Response.Write (" I am the output of default.aspx processing!") ); }

Effect:

  

Obviously, Excute can send the request to another handler to process the page again, and when the request is complete, it will return to this page to continue execution, the request context, including the form data, the URL information will be transmitted in the past. The Server.Execute method allows the current ASPX page to execute a specified ASPX page on the same Web server, and when the specified ASPX page finishes executing, the control flow returns to the original page where the Server.Execute call was made.

Server.Transfer is similar to the Response.Redirect method, except that the Response.Redirect function is similar, except that Response.Redirect is the client sending the request to the server, After the server executes Response.Redirect, returns 302, after the browser receives the request, sends the request again to the URL to jump, but the Server.Transfer jump is completely on the server side, the browser does not know has already jumped, therefore the path displays still is the original path.

In the case of better network status, the Redirect (URL) method is the most efficient!! aspx or non-ASPX (HTML) resources that can be redirected to the same or not the same server Server.Transfer methods and Server.Execute methods are the most flexible!! However, you can only go to the same application directory, and it may cause undesirable results to occur Server.Execute method consumes the most resources.

TEXT.ASHX Code:

        void ProcessRequest (HttpContext context)        {            context. Server.Transfer ("/default.aspx");}    

Default.aspx code:

        void Page_Load (object sender, EventArgs e)        {            Response.Write (" I am the output of default.aspx processing!") ); }

Effect:

  

Test.ashx page Code:

        Publicvoid ProcessRequest (HttpContext context) {strin G URL = http://www.xxx.com/username=zhangsan&age = "; Context. Response.Write (context. Server.URLEncode (URL)); //url path encoding context. Response.Write ( "<br/>  "); string encodeurl =  " Http%3a%2f%2fwww.xxx.com%2fusername%3dzhangsan%26age%3d " ; context. Response.Write (context. Server.urldecode (Encodeurl)); } 

Display effect:

  

      

Server object, HttpServerUtility class, getting server information

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.