在做.NET编程时,因为目录结构问题往往很难使用相对路径,而用应用程序的URL绝对路径来代替。但这样会涉及到应用程序名称,端口问题,我大概写了一个获取URL路径的方法
URL绝对路径
#region 生成url应用程序绝对路径
string absPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
absPath = absPath.Substring(System.Web.HttpContext.Current.Request.ApplicationPath.Length);
string asbUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
this._apppath = asbUrl.Substring(0, asbUrl.IndexOf(absPath));
this._apppath.TrimEnd('/');
#endregion
string absPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
absPath = absPath.Substring(System.Web.HttpContext.Current.Request.ApplicationPath.Length);
string asbUrl = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
this._apppath = asbUrl.Substring(0, asbUrl.IndexOf(absPath));
this._apppath.TrimEnd('/');
#endregion
硬盘物理路径和皮肤路径
#region 生成应用程序硬盘物理路径
this._physicalpath = System.Web.HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString().TrimEnd('\\');
#endregion
#region 生成url应用程序皮肤绝对路径
System.Web.Configuration.PagesSection _ps = (System.Web.Configuration.PagesSection)System.Configuration.ConfigurationManager.GetSection("system.web/pages");
this._themename = _ps.Theme;
this._fullthemepath = string.Format("{0}/App_Themes/{1}", this._apppath, _ps.Theme).TrimEnd('/');
#endregion
this._physicalpath = System.Web.HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"].ToString().TrimEnd('\\');
#endregion
#region 生成url应用程序皮肤绝对路径
System.Web.Configuration.PagesSection _ps = (System.Web.Configuration.PagesSection)System.Configuration.ConfigurationManager.GetSection("system.web/pages");
this._themename = _ps.Theme;
this._fullthemepath = string.Format("{0}/App_Themes/{1}", this._apppath, _ps.Theme).TrimEnd('/');
#endregion