asp.net 各种路径查找
记性不好,好多次都被路径问题给拦住了.我现在想得到一个资源的全URL路径,因此首先想得到网站当前的域名,端口和虚拟目录.看下表
底下這張表就是各種跟 Browser Request 的網址相關的屬性與用法:
網址:http://localhost:1897/News/Press/Content.aspx/123?id=1#toc | |
Request.ApplicationPath | / |
Request.PhysicalPath | D:\Projects\Solution\web\News\Press\Content.aspx |
System.IO.Path.GetDirectoryName(Request.PhysicalPath) | D:\Projects\Solution\web\News\Press |
Request.PhysicalApplicationPath | D:\Projects\Solution\web\ |
System.IO.Path.GetFileName(Request.PhysicalPath) | Content.aspx |
Request.CurrentExecutionFilePath | /News/Press/Content.aspx |
Request.FilePath | /News/Press/Content.aspx |
Request.Path | /News/Press/Content.aspx/123 |
Request.RawUrl | /News/Press/Content.aspx/123?id=1 |
Request.Url.AbsolutePath | /News/Press/Content.aspx/123 |
Request.Url.AbsoluteUri | http://localhost:1897/News/Press/Content.aspx/123?id=1 |
Request.Url.Scheme | http |
Request.Url.Host | localhost |
Request.Url.Port | 1897 |
Request.Url.Authority | localhost:1897 |
Request.Url.LocalPath | /News/Press/Content.aspx/123 |
Request.PathInfo | /123 |
Request.Url.PathAndQuery | /News/Press/Content.aspx/123?id=1 |
Request.Url.Query | ?id=1 |
Request.Url.Fragment | |
Request.Url.Segments | / News/ Press/ Content.aspx/ 123 |
// 获取程序的基目录。 System.AppDomain.CurrentDomain.BaseDirectory // 获取模块的完整路径。 System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName // 获取和设置当前目录(该进程从中启动的目录)的完全限定目录。 System.Environment.CurrentDirectory // 获取应用程序的当前工作目录。 System.IO.Directory.GetCurrentDirectory() // 获取和设置包括该应用程序的目录的名称。 System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase // 获取启动了应用程序的可执行文件的路径。 System.Windows.Forms.Application.StartupPath // 获取启动了应用程序的可执行文件的路径及文件名 System.Windows.Forms.Application.ExecutablePath C# WinForm中AppDomain.CurrentDomain.BaseDirectory与Application.StartupPath的区别示例如下: private void Frm_Server_Load(object sender, EventArgs e) { MessageBox.Show(AppDomain.CurrentDomain.BaseDirectory); MessageBox.Show(Application.StartupPath ); } 说明: 1. AppDomain.CurrentDomain.BaseDirectory 返回结果为: D:\mycode\ Application.StartupPath 返回结果为: D:\mycode 2. Application.StartupPath 只能用于WinForm窗体中,而AppDomain.CurrentDomain.BaseDirectory既可以用于WinForm窗体中,也可以用于类库DLL文件中. 3. HttpContext.Current.Request.Url.Host.ToString() 返回结果为:http://locahost.XXXX.XXX HttpContext.Current.Server.MapPath(); System.Web.HttpContext.Current.Request.PhysicalApplicationPath