Asp. Net intermediate-1. Virtual Path + 2. Request object + 3. Response object + 4. Server Object

Source: Internet
Author: User
Directory:

1. Virtual Path

1. Special path identifier "~" (~ Can only be used in server controls)

1.2. For example, use ~, Instead. WebSite.

1. 3. Programming "~"

2. Request object

Differences between Server. MapPath () and Request. MapPath () in ASP. NET

3. Response object

4. Server Object

Example:Server. Transfer (path)

 

1. Virtual Path 1. 1. Special path ID "~" (~ Can only be used in server controls)

Different from Http standard positioning such as "/indicates the website root directory (domain name),.../indicates the parent directory, and./indicates the current directory ,~ It is a special symbol defined by ASP. Net. It is used to recommend the definitions within ASP. Net ~ Start from the application root directory. The difference between the application root directory and the WebSite root directory is that if you deploy an application to a WebSite (create a WebSite for demonstration, because different websites are under the same WebSite root directory ), therefore, it is best to use "~", "~" ASP. Net converts the path to a full path relative to the root directory of the website and then outputs it to the browser.

<A href = "/a. aspx">/path </a>

<A href = ".../a. aspx"> ../path </a>

<A href = "./a. aspx">./path </a>

 

 

 

1.2. For example, use ~, Instead. WebSite.

1. If you use

Http: // localhost: 4217/virtual address/virtual path/Default. aspx

<Asp: HyperLink ID = "HyperLink1" runat = "server" NavigateUrl = "http://www.cnblogs.com/Cookie1.aspx"> HyperLink </asp: HyperLink>

2. Move to this location

Http: // localhost: 4217/virtual address/Default. aspx

3. You cannot find this address.

If you use ~, This is a good solution.

<Asp: HyperLink ID = "HyperLink1" runat = "server" NavigateUrl = "~ /Cookie1.aspx "> HyperLink </asp: HyperLink>

 

 

 

1. 3. Programming "~"
  • If you use the runat = server Control in the server control, For conversion, if you want to convert HTML controls or code, you can use static methods in the VirtualPathUtility class to convert virtual paths and full paths, such as VirtualPathUtility. ToAbsolute ("~ /A/b1.aspx ") is to convert the virtual path to the full path relative to the application root, that is,/WebSite4/a/b1.aspx
  • (*) Main methods of the VirtualPathUtility class: string AppendTrailingSlash (string virtualPath): If the path virtualPath does not end with "/", add it; string Combine (string basePath, string relativePath ), merge the two paths. string GetDirectory (string virtualPath) returns the directory part of the virtual path. string MakeRelative (string fromPath, string toPath) calculates the relative paths of the two virtual paths. ToAbsolute, convert to absolute path

 

 

2. Request object
  • 1 ,(*)Request. AppRelativeCurrentExecutionFilePathTo obtain the virtual path of the current execution request relative to the application root directory ~ For example, /Handler. ashx ",
Response. Write ("Get the virtual path AppRelativeCurrentExecutionFilePath:" + Request. AppRelativeCurrentExecutionFilePath relative to the application root directory );

  • 2 ,(*)Request. PhysicalApplicationPathTo obtain the physical path of the current application, such as D: \ My Documents \ Visual Studio 2008 \ WebSites \ WebSite4 \
  • 3 ,(*)Request. PhysicalPathTo obtain the physical path of the current request, such as D: \ My Documents \ Visual Studio 2008 \ WebSites \ WebSite4 \ Handler. ashx
  • 4 ,(*)Request. RawUrlObtain the original Request URL and Request. Url to obtain the Request URL. The difference involves URL rewriting.
  • 5,Request. UrlReferrerThe source of the web page can be determined based on this keyword searched by Baidu, anti-download leeching, anti-image leeching, and can be forged (such as thunder ). "This image is only for internal communication of rupeng network", tested in DZ. Globals. asax for global anti-leech Protection
  • 6,Request. UserHostAddressObtain the visitor's IP address
  • 7 ,(*)Request. useragesThe language supported by the visitor's browser can be used to display pages in different languages to people in different languages.
  • 8,Request. CookiesObtain the Cookie sent from the browser and read the Cookie value from it, such as context. request. cookies ["mysessionid"], using Request. cookies are generally read only. Write the Cookies back to the browser and use Response. cookies
  • 9,Request. MapPath (virtulPath)Converts a virtual path to a physical path on the disk (equivalent to Server. Mappath ())
    Request. MapPath ("~ /A/B. aspx "), you will get D: \ 2008 \ WebSites \ WebSite4 \ a \ B. aspx
Differences between Server. MapPath () and Request. MapPath () in ASP. NET

Server. MapPath (string): maps the files (or directories) of the currently called files to physical paths;

Request. MapPath (string): ing the string virtual path to the physical path (Request does not have this method in asp)

In Server. MapPath (string), the string can reference the parent directory in the "../" mode, or even jump this directory out of the entire WEB directory, such as: C: \ WWWROOT

The directory is the WEB root directory. You can call this Server. MapPath ("../xyz.gif") in the root directory file to call scripts and resources outside the WEB directory.

Request. the string in MapPath (string) is a virtual directory, which can only be in the form of a WEB virtual directory and cannot be ".. /"method call, can only be"/","/xx "and other strings

Sometimes directly use Server. mapPath (string) is troublesome to call a file because the same Server is called in different directories. the MapPath (string) function will get different values. In special cases, you need to determine the directory level to obtain the correct address, using the Request. mapPath (string) can call the same directory file. No directory judgment required

 

 

3. Response object
  • Response Buffer output: ASP. by default, Net will not immediately output data to the browser every time it writes data to the browser. Instead, it will cache the data and send the data in the buffer to the browser at the appropriate time or after the response ends.
  • Main Member of the Response object:
  • 1,Response. Buffer, Response. BufferOutput: After Reflector decompilation, it is found that the two attributes are the same, and Buffer is called BufferOutput internally. This attribute is used to control whether the response cache is used. The default value is true.
  • 2,Response. Flush ()Send data in the buffer to the browser. This is applicable when you need to output the written content to the browser immediately. Case: The import of large batches of data shows that the * data is being imported, and Thread. Sleep is used to simulate the time consumption.
  • 3,Response. Clear ()Clear the data in the cache, so that the data that is not sent to the browser in the cache is cleared and not sent to the browser. In the example of outputting non-html data using aspx, clear is often used to output additional content such as httpmodule. (It is not recommended that ashx be used)
  • 4,Response. ContentEncodingEncoding of the output stream.
  • 5,Response. ContentTypeThe content type of the output stream, such as html (text/html), plain text (text/plain), or JPEG image (image/JPEG ).
  • 6,Response. CookiesSet of cookies returned to the browser.
  • 7,Response. OutputStreamThe output stream is used to output non-text content such as images and Excel files.
  • 8,Response. End ()Terminate the response and send the cached data to the browser. The code after End () is not executed. When terminating some illegal requests, such as leeching, you can use End () to immediately terminate the request.
  • 9,Response. Redirect (url)Redirect the browser to the new URL. You can redirect to an out-of-site website or an in-site website. Response. Redirect ("http://www.rupeng.com"), Response. Redirect ("a.htm "). Redirect sends a 302 redirection to the browser, notifying the browser that "please visit the url again ", this process has gone through the process of notifying the browser "please visit the url again" and the browser to access the new url through commands. Use HttpWatch to view the Http packet of the entire response process. Because Redirect is used by the browser to re-access the new URL, you can see the changes in the URL in the address bar. It will be used later to prevent the prompt "retry" When refreshing the browser ".
  • 10,Response. SetCookie (HttpCookie cookie ),Update the Cookie written to the browser in the output stream. If the Cookie exists, the update does not exist. Is a simplified call to Response. Cookies.
  • 11,Response. Write ()Output content to the browser.
  • 12 ,(*)Response. WriteFile (filename)Output the file to the browser. For example, Response. WriteFile ("c:/boot. ini ")

 

 

4. Server Object
  • Server isContextIsHttpServerUtilityClass Object
  • Server. HtmlDecode (),Server. HtmlEncode () Server. UrlEncode (),Server. UrlDecode ()Is a proxy call to the corresponding methods in the HttpUtility class. I personally recommend that you always use HttpUtility, because it is difficult to obtain the Server Object in some places. Do not mix HtmlEncode and UrlEncode. UrlEncode processes hyperlinks. HtmlEncode processes html code.
  • Server. Transfer (path)Internal redirect request, Server. transfer ("JieBanRen. aspx ") Redirects user requests to JieBanRen. aspx processing is the internal takeover of the server, and the browser cannot realize this takeover, not like Response. redirect goes through the process of "notifying the browser 'Please visit the url again 'and the browser to access the new url through commands", which is an http request, so the address bar of the browser will not change (Lenovo, the call center agent informs the customer of the difference between a number and a number for Customer Transfer ). Because it is internally taken over, the redirected page can access parameters accepted by Request, Cookies, and other source pages, just as these parameters are passed to him, redirect does not work because it allows the browser to access it. Note that Transfer is an internal takeover, so it cannot be redirected to an external website like Redirect. (Regular)
Example:

Tansfer. aspx

protected void Page_Load(object sender, EventArgs e)    {        string q = Request["q"];        if (q == "1")        {            Server.Transfer("http://www.baidu.com");        }        else if(q =="2")        {            Response.Redirect("http://www.baidu.com");        }    }

Receive. aspx

protected void Page_Load(object sender, EventArgs e)    {       Response.Write( Request["q"]);    }
  • Server. Transfer cannot be directly redirected to ashx. Otherwise, an error is returned, indicating that the subrequest is executed incorrectly ".
  • Sometimes you cannot get the HttpContext object. For example, in Global. asax (later), you can get the Current HttpContext through HttpContext. Current, and then get the Response, Request, Server, and so on.
  • Server. MapPath. If the Server cannot be obtained, use the HostingEnvironment. MapPath () method.

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.