Asp.net built-in Object Server Object (overview and Application)

Source: Internet
Author: User

1. Understand Server objects

The Server object provides access to methods and attributes on the Server and HTML encoding. These functions are completed by the corresponding methods and attributes of the Server object.

Ii. common attributes of Server objects

(1). MachineName
(2). ScriptTimeout: This attribute is used to set the script execution time. Setting ScriptTimeout appropriately improves the efficiency of the entire Web application. Syntax:
Server. ScriptTimeout = time; (in seconds)

The minimum time of the ScriptTimeout attribute is 90 s by default. This value is sufficient for script programs with simple logic and less activity content. However, when executing script programs with a large amount of activity content, it seems smaller. For example, the script program accessing the database must set a large ScriptTimeout attribute value. Otherwise, the script program cannot be executed normally.

Copy codeThe Code is as follows: Response. Write ("Server machine name:" + Server. MachineName); // Server machine name: IBM
Response. Write ("Timeout:" + Server. ScriptTimeout); // Timeout: 30000000

Iii. Main Methods of Server objects

Iv. common applications of Server objects:

(1) execute other ASP. NET web pages

The Execute method of the Server object can be used to Execute another page on the same Web Server on the current page. After the page is executed, the control process will return to the original page to issue the Server. the location where the Execute method is called. The called page should be a. aspx webpage. Therefore, the output result of one. aspx page can be inserted into another. aspx page through the Server. Execute method call. The syntax of Server. Execute is as follows:
Server. Execute (path)

On the WebForm1 page:

Copy codeThe Code is as follows: protected void Page_Load (object sender, System. EventArgs e)
{
Response. Write ("<P> before calling the Execute method </P> ");
Server. Execute ("Page2.aspx"); // use Server. Execute (Path) to Execute other ASP. NET pages. Insert the output result of Page2.aspx to the current page.
// Server. Execute ("http://www.163.com"); // The program cannot be executed and must be a relative path
Response. Write ("<P> after the Execute method is called </P> ");
}

On the page. aspx pageCopy codeThe Code is as follows: protected void Page_Load (object sender, System. EventArgs e)
{
Response. Write ("This Is A page2.aspx webpage ");
}

The running result is:

(2) Transfer Process Control to other ASP. NET web pages

The Transfer method of the Server object can terminate the execution of the current page and Transfer the execution process to another page of the same Web Server. The called page should be. on the aspx page, the information stored by the Request and other objects remains unchanged during the page Jump process. This means that the data submitted in page A can be used after page A jumps to page B. In addition. the Transfer method is called on the server. The client browser does not know that the server has executed A page Jump. Therefore, after the page Jump is implemented, the browser address bar will still save the URL Information of page, in this way, unnecessary network communication can be avoided to achieve better performance and browsing performance. The method of Server. Transfer is as follows:
Server. Transfer (path)

Prompt: The path parameter specifies the URL path of the new page to be executed on the server. Some query string variable name/value pairs can be appended to this URL.
On the WebForm1 page:

Copy codeThe Code is as follows: protected void Page_Load (object sender, System. EventArgs e)
{
Response. Write ("<P> before calling the Transfer method </P> ");
// Response. Redirect ("Page2.aspx ");
Server. Transfer ("Page2.aspx ");
// Use Server. Transfer (path) to terminate the execution of the current page. Therefore, when this line is executed, the execution is terminated, that is, the subsequent execution is no longer performed;
// Then transfer the execution process to another page of the same Web server. That is, in the page2.aspx page. During the page Jump process, the information stored by the Request and other objects remains unchanged.
// This means that the data submitted in page A can be used after page A jumps to page B: the data before Server. Transfer (path) can be output on the page page2.apsx.
// Server. Transfer ("www.163.com"); // No. It can only be a virtual path.
Response. Write ("<P> after the Transfer method is called </P> ");
}

On the Page2 page:Copy codeThe Code is as follows: protected void Page_Load (object sender, System. EventArgs e)
{
Response. Write ("<P> This is the execution result of Page2.aspx </P> ");
}

The running result is as follows:

If Response. Redirect ("Page2.aspx") is changed, the page Jump function can only be implemented.
Reference: three methods for passing values on ASP. NET pages

Copy codeThe Code is as follows: (1). Use QueryString
(2). Use Session Variables
(3). Use Server. Transfer

(3). convert a virtual path to a physical file path

You often need to access files or folders on Web forms pages. In this case, you must convert a virtual path to a physical file path. The MapPath method maps the specified relative or virtual path to the corresponding physical directory on the server. Multiple Web applications on the Web server are generally stored in different directories according to their respective functions.

After a virtual directory is used, the client can still use a virtual path to access the webpage, which is a common webpage URL of Internet users in browsers, however, you cannot know the actual path of the webpage (actual storage location ). However, if you really need to know the actual path of a webpage file, you can use the MapPath method. The syntax of the MapPath method is as follows:

Server. MapPath (Path)

Note: The Path parameter specifies the relative or virtual Path to map to the physical directory. After the MapPath method is executed, the physical file path corresponding to the path is returned.
Confused: relative path, absolute path, physical path, virtual path

Copy codeThe Code is as follows: (1) Relative Path: relative path to the current directory, or relative path to a directory, which mainly reflects the concept of "relative ".
(2) absolute path: the path starting from the root path of the Website to the highest level, for example, C: \ Website \ web1 \ index.html
(3). physical path: the path in the actual disk, either relative or absolute.
(4). Virtual Path: the path mapped by the server, such as/myweb

(Here cited CSDN Boyou: flytosea8 explanation: link address: http://bbs.csdn.net/topics/70115404)

Virtual Path and physical path
Here is an example of IIS:
The WEB service directory is d: \ test
When using HTTP to access the root directory of the website, the actual access is d: \ test. Then, the virtual path is \ (Root), and the physical path is d: \ test.

Generally, virtual paths and physical paths are commonly used in WEB and FTP services.

Relative paths and absolute paths are widely used.
Relative Path: for example, D: \ TEST1 \ TEST2 \ TEST3
TEST2 is the lower-level path of TEST1, and TEST2 is the higher-level path of TEST3. Therefore, both the higher-level path and the lower-level path are relative. Generally, the upper-level path can be represented by..., and the current path can be represented.
Absolute path: for example, the c: \ root path is absolute, and any path has an absolute closest path relative to the root path, which is also an absolute path.

If you think about how others are leading the way, you can find out what is relative and what is absolute.
Relative method: move forward from "here" and turn right
Absolute guidance: The bus station is 100 to the East (if the bus station is an absolute location), the absolute guidance method does not depend on where the person is

Copy codeThe Code is as follows: protected void Page_Load (object sender, System. EventArgs e)
{
Response. Write ("the root directory of the Web site is:" + Server. MapPath ("/") + "<br> ");
Response. write ("the actual path of the current virtual directory is:" + Server. mapPath (". /") +" <br> "); // E: \ asp.net \ Server Object \ code \
Response. write ("the actual path of the current webpage is:" + Server. mapPath (Request. filePath) + "<br>"); // E: \ asp.net \ Server Object \ code \ WebForm1.aspx
Response. write ("the actual path of the current webpage is:" + Server. mapPath ("webform2.aspx") + "<br>"); // E: \ asp.net \ Server Object \ code \ WebForm2.aspx

}

(4) encoding and decoding of strings

In some cases, you may need to display content such as "paragraph mark <p>" on the webpage, instead of interpreting <p> as a paragraph mark in HTML; in the above cases, the HtmlEncode method of the Server object should be called to encode the string to be displayed in the browser
Sometimes, when passing parameters, the data is appended to the URL for transmission, but if there are some special characters such as "#", the parameters following these characters will not be read. Therefore, when passing special characters, you must encode the content to be passed with UrlEncode to ensure that the passed value can be read smoothly.
In addition, some servers do not provide good support for Chinese characters. At this time, UrlEncode must be used for encoding to be recognized by the server.

Copy codeThe Code is as follows: HtmlEncode | HtmlDecode:
Protected void Page_Load (object sender, System. EventArgs e)
{
Response. write (Server. htmlEncode ("bold: <B> bold text </B>"); // HtmlEncode encodes the string in HTML format and returns the encoded string.
Response. Write ("<br> ");
Response. write (Server. htmlDecode ("marked in bold as <B> bold text </B>"); // HtmlDecode decodes the string in HTML format and returns the decoded string.
}

Copy codeThe Code is as follows: UrlEncode | UrlDecode:
Protected void Page_Load (object sender, System. EventArgs e)
{
Response. Write ("<A href = '1. aspx? Data = "+ Server. urlDecode ("name @ # 163.com") +" '> unencoded parameter content </A> <br> "); // at 1. aspx page output: name @
Response. Write ("<A href = '1. aspx? Data = name @ # 163.com '> unencoded parameter content </A> <br> "); // output on the 1. aspx page: name @
Response. Write ("<A href = '1. aspx? Data = "+ Server. urlEncode ("name @ # 163.com") +" '> encode parameter content </A> <br> "); // at 1. aspx page output: name @ # 163.com
Response. Write (Server. UrlDecode ("name @ # 163.com"); // decodes the URL of the string. The output is name @ # 163.com.
Response. Write ("<br> ");
Response. Write (Server. UrlEncode ("name @ # 163.com"); // encode the URL of the string. The output is name % 40% 23163.com.
Response. Write ("<br> ");
Response. Write (Server. UrlDecode (""); // output: Chinese
Response. Write ("<br> ");
Response. Write (Server. UrlEncode ("Chinese"); // output: % e4 % b8 % ad % e6 % 96% 87
Response. Write ("<br> ");
Response. Write (Server. UrlEncode ("english"); // output: english
Response. Write ("<br> ");
Response. Write (Server. UrlDecode ("english"); // output: english
}

Related Article

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.