国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET編程獲取網(wǎng)站根目錄方法小結(jié)

2024-07-10 12:47:39
字體:
供稿:網(wǎng)友

本文實例講述了ASP.NET編程獲取網(wǎng)站根目錄方法。,具體如下:

獲取網(wǎng)站根目錄的方法有幾種如:

Server.MapPath(Request.ServerVariables["PATH_INFO"])
Server.MapPath("/")
Server.MapPath("")//當前代碼文件所在的目錄路勁
Server.MapPath(".")
Server.MapPath("../")
Server.MapPath("..") 
Page.Request.ApplicationPath

以上的代碼在http://localhost/EnglishClub/manage/WebForm1.aspx頁面

運行結(jié)果:

C:/Inetpub/wwwroot/EnglishClub/manage/WebForm1.aspx
C:/Inetpub/wwwroot/
C:/Inetpub/wwwroot/EnglishClub/manage
C:/Inetpub/wwwroot/EnglishClub/manage
C:/Inetpub/wwwroot/EnglishClub/
C:/Inetpub/wwwroot/EnglishClub

以上的方法可以在.aspx中訪問,但是如果你在。cs文件就不能用。

HttpContext.Current.Server.MapPath();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath

在.cs文件中可以用。但是HttpContext.Current.Server.MapPath();這個獲取的是文件的路徑而不是根目錄。

只有System.Web.HttpContext.Current.Request.PhysicalApplicationPath 這個才是獲取的根目錄,在寫獲取數(shù)據(jù)庫路徑是應(yīng)該用這個,其他的都有問題。

System.Web.HttpContext.Current.Request.PhysicalApplicationPath
和Server.MapPath("~/")效果是一樣的。

Server.MapPath("~/");//無論代碼所在的文件的、頁面路勁是什么,永遠返回 C:/Inetpub/wwwroot/EnglishClub/(就是當前程序運行的所在根目錄)

如果存儲 附件的路勁 進數(shù)據(jù)庫的話,不應(yīng)該把絕對路勁存進去。應(yīng)該只存儲 文件名部分。例如:

/uploads/abc.txt
當需要瀏覽文件的時候,在在讀取出來的路徑:(即/uploads/abc.txt),前面+網(wǎng)站的路勁:例如:

http://abc.com+"/uploads/abc.txt"

補充:

ASP.NET中獲取網(wǎng)站根目錄和物理路徑完整實例:

/// <summary>/// 取得網(wǎng)站的根目錄的URL/// </summary>/// <returns></returns>public static string GetRootURI(){  string AppPath = "";  HttpContext HttpCurrent = HttpContext.Current;  HttpRequest Req;  if (HttpCurrent != null)  {    Req = HttpCurrent.Request;    string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);    if (Req.ApplicationPath == null || Req.ApplicationPath == "/")      //直接安裝在  Web  站點        AppPath = UrlAuthority;    else      //安裝在虛擬子目錄下        AppPath = UrlAuthority + Req.ApplicationPath;  }  return AppPath;}/// <summary>/// 取得網(wǎng)站的根目錄的URL/// </summary>/// <param name="Req"></param>/// <returns></returns>public static string GetRootURI(HttpRequest Req){  string AppPath = "";  if(Req != null)  {    string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority);    if (Req.ApplicationPath == null || Req.ApplicationPath == "/")      //直接安裝在  Web  站點        AppPath = UrlAuthority;    else      //安裝在虛擬子目錄下        AppPath = UrlAuthority + Req.ApplicationPath;  }  return AppPath;}/// <summary>/// 取得網(wǎng)站根目錄的物理路徑/// </summary>/// <returns></returns>public static string GetRootPath(){  string AppPath = "";  HttpContext HttpCurrent = HttpContext.Current;  if (HttpCurrent != null)  {    AppPath = HttpCurrent.Server.MapPath("~");  }  else  {    AppPath = AppDomain.CurrentDomain.BaseDirectory;    if (Regex.Match(AppPath, @"http://$", RegexOptions.Compiled).Success)      AppPath = AppPath.Substring(0, AppPath.Length - 1);  }  return AppPath;}            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 昌吉市| 黄骅市| 武穴市| 大关县| 红桥区| 广东省| 扎兰屯市| 河西区| 涡阳县| 利川市| 定结县| 沙河市| 白城市| 绥化市| 朝阳市| 金寨县| 房山区| 楚雄市| 偃师市| 永济市| 山西省| 云霄县| 休宁县| 安龙县| 拜泉县| 三穗县| 即墨市| 荃湾区| 炉霍县| 房产| 高雄县| 洛川县| 沙雅县| 吉木萨尔县| 调兵山市| 综艺| 清镇市| 库尔勒市| 麻栗坡县| 静海县| 苏尼特右旗|