asp.net access to the absolute path of the site case detailed

Source: Internet
Author: User

  in writing asp.net applications, sometimes in order to better control the path of static files, such as in the template page or user control set JS or CSS file path, etc., using absolute path is unavoidable. Here are a few ways to get an absolute path

  Code as follows: Virtualpathutility.toabsolute ("~/") Httpruntime.appdomainappvirtualpath Request.applicationpath Page.resolveurl ("~")     The results of the above code generation are as follows: When accessing as a Web site, the results are as follows:      code is as follows: Virtualpathutility.toabsolu Te ("~/") =/Httpruntime.appdomainappvirtualpath =/Request.applicationpath =/Page.resolveurl ("~") =/    when Virtual directory (http://localhost:806/web2/url.aspx) access, the result is as follows:   code is as follows: Virtualpathutility.toabsolute ("~/") =/web2/ Httpruntime.appdomainappvirtualpath =/web2 Request.applicationpath =/web2 Page.resolveurl ("~") =/web2/    Mining With the second and third methods, you also need to do some processing, because the Web site access is/end, while the virtual directory access is not/, do have to make a judgment, a little trouble. However, there is no problem with using these methods on the page, but if you need to get an absolute path to the site in the Global Application_Start event, you need to use the first 2 methods, and if you use the third method, you will report the following error: Request is not Available in this context  so you can only take the first 2 ways. For example, the   code is as follows: void Application_Start (object sender, EventArgs e) {    System.IO.StreamWriter s = new SYSTEM.I O.streamwriter (Httpruntime.appdomainapppath + "Log.txt"); &nBsp   S.writeline (Virtualpathutility.toabsolute ("~/"));     S.writeline (Httpruntime.appdomainappvirtualpath);     S.close (); }  

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.