Here are a few ways to get an absolute path.
Virtualpathutility.toabsolute ("~/") Httpruntime.appdomainappvirtualpath Request.applicationpath Page.resolveurl ("~") |
The above code produces the following results:
when accessed as a Web site, the results are as follows:
Virtualpathutility.toabsolute ("~/") =/
Httpruntime.appdomainappvirtualpath =/
Request.applicationpath =/
Page.resolveurl ("~") =/
when accessed as a virtual directory (http://localhost:806/web2/url.aspx), the results are as follows:
Virtualpathutility.toabsolute ("~/") =/web2/
Httpruntime.appdomainappvirtualpath =/web2
Request.applicationpath =/web2
Page.resolveurl ("~") =/web2/
the second and third methods, but 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, these methods are not problematic to use 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:
The Request isn't available in the context
So, you can only take the first 2 ways. such as
void Application_Start (object sender, EventArgs e) { System.IO.StreamWriter s = new System.IO.StreamWriter (Httpruntime.appdomainapppath + "Log.txt"); S.writeline (Virtualpathutility.toabsolute ("~/")); S.writeline (Httpruntime.appdomainappvirtualpath); S.close (); } |