ASP.net built-in Object Server objects (Overview and application) _ Practical Tips

Source: Internet
Author: User
Tags urlencode

First, understand the server object

The server object provides access to methods and properties on the server, as well as HTML-encoded functionality. These functions are completed by the corresponding methods and properties of the server object.

Ii. Common properties of server objects

(1). MachineName
(2). ScriptTimeout: Properties are used to set the time of execution of a script program, and the proper setting of ScriptTimeout for scripting can improve the efficiency of the entire Web application. The syntax is as follows:
Server.scripttimeout=time (in S (seconds))

The shortest time for the ScriptTimeout property defaults to S. This value is sufficient for some script programs that are simple in logic and have less activity content. But it's a little bit smaller when it comes to executing more scripted programs with more active content. For example, access to the database script program, you must set a large ScriptTimeout property value, otherwise the script program will not perform properly.

Copy Code code as follows:

Response.Write ("Server machine Name:" + server.machinename);//server machine name: IBM
Response.Write ("timeout:" + server.scripttimeout);/timeout: 30000000

The main method of the server object

Iv. Common applications of server objects :

(1). Perform other asp.net web pages

The Execute method of the server object enables you to execute another page on the same Web server in the current page, and when the page is finished, the control process is returned to the original page where the Server.Execute method call was issued. The called page should be an. aspx Web page, so you can insert the output of an. aspx page into another. aspx page by using the Server.Execute method call. The Server.Execute method syntax is as follows:
Server.Execute (PATH)

In the WebForm1 page:

Copy Code code as follows:

protected void Page_Load (object sender, System.EventArgs e)
{
Response.Write ("<P> before calling Execute method </P>");
Server.Execute ("Page2.aspx");//Use Server.Execute (Path) to perform other asp.net pages. This inserts the page2.aspx output into the current page
Server.Execute ("http://www.163.com");/the program cannot execute, must be a relative path
Response.Write ("<P> after calling Execute method </P>");
}

In the Page.aspx page
Copy Code code as follows:

protected void Page_Load (object sender, System.EventArgs e)
{
Response.Write ("This is the Page2.aspx page");
}

The result of the run is:

(2). Transfer process Control to other ASP.net web pages

Using the transfer method of the server object, you can terminate the execution of the current page and transfer the execution process to another page on the same Web server. The called page should be an. aspx page, and the information kept by the request and so on is unchanged during page jumps, which means that the data submitted in page A can continue to be used after you jump from page A to page B. Furthermore Because the Server.Transfer method call is done on the server side, the client browser does not know that the server side has executed a page jump, so the browser address bar will still save the URL information of page a after the page jump, so that you can avoid unnecessary network traffic, so as to achieve better performance and browsing effect. The Server.Transfer method is as follows:
Server.Transfer (PATH)

tip : Parameter path Specifies the URL path of a new page to be executed on the server, which can also be appended with the name/value pairs of the query string variable
In the WebForm1 page:

Copy Code code as follows:

protected void Page_Load (object sender, System.EventArgs e)
{
Response.Write ("<P> call Transfer method before </P>");
Response.Redirect ("Page2.aspx");
Server.Transfer ("Page2.aspx");
Use Server.Transfer (path) to terminate execution of the current page, so execution to this line terminates, meaning that the latter is no longer executing;
The execution process is then transferred to another page of the same Web server. That's the Page2.aspx page. In the page jump process, the request and other objects to save the information unchanged
This means that you can continue to use the data submitted in page A after you jump from page A to page B: Then you can output the data before Server.Transfer (path) in the PAGE2.APSX page.
Server.Transfer ("www.163.com");/This is not possible, only virtual path
Response.Write ("</P> after <P> call Transfer method");
}

On the Page2 page:
Copy Code code as follows:

protected void Page_Load (object sender, System.EventArgs e)
{
Response.Write ("<P> This is the result of page2.aspx implementation </P>");
}

Then run the result:

If we change to Response.Redirect ("Page2.aspx"), then only the page jump function can be implemented.
Quote: Three ways to asp.net page transfer values

Copy Code code as follows:

(1). Using QueryString
(2). Using Session Variables
(3). Using Server.Transfer

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

When you frequently need to access files or folders in a Web Forms page, you often require that you 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 in a Web server are typically stored in different directories according to their different functions.

With virtual directories, clients can still access Web pages using virtual paths, which is the URL of a Web page that is common to Internet users in a browser, but at this point the user cannot know the actual path of the page (the actual location). However, if you do need to know the actual path of a Web page file, you can use the MapPath method. The syntax for the MapPath method is as follows:

Server.MapPath (Path)

Note: The parameter path indicates the relative or virtual path to which you want to map the physical directory. After the MapPath method is executed, the physical file path corresponding to path is returned.
FAQ: Relative path, absolute path, physical path, virtual path

Copy Code code as follows:

(1). Relative path: The path to the current directory, or the path relative to a directory, where the concept of "relative" is mainly embodied
(2). Absolute path: A path starting at the highest level from the root path of the site, such as: C:\Website\web1\index.html
(3). Physical path: A path on the actual disk that can be a relative path or an absolute path
(4). Virtual path: A path that is mapped by the server, such as/myweb

(quoted in this csdn Bo friends: Flytosea8 Explanation: Link address: http://bbs.csdn.net/topics/70115404)


Virtual path, physical path
Use IIS for an example:
Web Services Directory is d:\test
Then use HTTP to access the site root directory, in fact, access is d:\test, then the virtual path is \ (root), the physical path is D:\test

Generally speaking, the reference of virtual path and physical path is more common in Web and FTP service.

Relative paths and absolute paths are more extensive and can be seen everywhere.
Relative path: Like D:\TEST1\TEST2\TEST3
Then TEST2 is the TEST1 path, TEST2 is TEST3 's superior path, so the superior path or lower path are relative. General superior path can be used. To indicate that the current path can be used.
Absolute path: For example C:\ root path is absolute, any path relative to the root path has an absolute nearest path, is also an absolute path.

If you think about other people's directions, you can realize what is relative and what is absolute.
Relative means of direction: from "Here" go forward, turn right on the
Absolute means of direction: 100 meters East (if the bus station is the absolute position), the absolute way of means does not depend on the road of the person where


Copy Code code 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 to the current virtual directory is:" +server.mappath ("./") + "<br>");//e:\asp.net\server object \code\
Response.Write ("The actual path to the current page is:" +server.mappath (Request.filepath) + "<br>");//e:\asp.net\server object \code\ WebForm1.aspx
Response.Write ("The actual path to the current page is:" +server.mappath ("webform2.aspx") + "<br>");//e:\asp.net\server object \code\ Webform2.aspx

}

(4). Encoding and decoding of strings

In some cases, you might want to display "paragraph mark <p>" in your Web page, but you don't want the browser to interpret <p> as a paragraph mark in the HTML language; You should call the HTMLEncode method of the server object to encode the string to be displayed in the browser
Sometimes, when passing parameters, the data is appended to the URL, but if you encounter some special characters such as "#", you will not be able to read the parameters following these characters. So it is necessary to pass the special characters, the first thing to be passed to the UrlEncode encoding, so as to ensure that the value passed can be read smoothly.
Other servers are not very good for Chinese support, this time also need to use UrlEncode to encode it to be recognized by the server.

Copy Code code as follows:

htmlencode| | HtmlDecode:
protected void Page_Load (object sender, System.EventArgs e)
{
Response.Write (Server.HTMLEncode ("Bold to:<b> bold text </B>");//htmlencode HTML-encode the string and return the encoded string, so this is the output
Response.Write ("<br>");
Response.Write (Server.htmldecode (Bold to:<b> bold text </B>))//htmldecode HTML decoding of strings and returns the decoded string, so this is bold behind
}

Copy Code code 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") + "' > No encoded parameter contents </A><br> ");//in 1.aspx page output: name@
Response.Write ("<a href= ' 1.aspx?data=name@ #163. com ' > No encoded parameter Content </A><br>");//in 1.aspx page output: name@
Response.Write ("<a href= ' 1.aspx?data=" + server.urlencode ("name@ #163. com") + "' > Encoded parameter Content </A><br>") ;//on 1.aspx page output: name@ #163. com
Response.Write (Server.urldecode ("name@ #163. com"))//URL-decode the string, output here: name@ #163. com
Response.Write ("<br>");
Response.Write (Server.URLEncode ("name@ #163. com"))//URL-coded for string, output here: name%40%23163.com
Response.Write ("<br>");
Response.Write (Server.urldecode ("Chinese"));//output: Chinese
Response.Write ("<br>");
Response.Write (Server.URLEncode ("Chinese"));//output:%e4%b8%ad%e6%96%87
Response.Write ("<br>");
Response.Write (Server.URLEncode ("中文版"));//output: 中文版
Response.Write ("<br>");
Response.Write (Server.urldecode ("中文版"));//output: 中文版
}

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.