您好,登錄后才能下訂單哦!
怎么在ASP.NET中獲取網(wǎng)站的根目錄?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
獲取網(wǎng)站根目錄的方法有幾種如:
Server.MapPath(Request.ServerVariables["PATH_INFO"])
Server.MapPath("/")
Server.MapPath("")//當(dāng)前代碼文件所在的目錄路勁
Server.MapPath(".")
Server.MapPath("../")
Server.MapPath("..")
Page.Request.ApplicationPath
以上的代碼在http://localhost/EnglishClub/manage/WebForm1.aspx頁(yè)面
運(yùn)行結(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中訪問(wèn),但是如果你在。cs文件就不能用。
HttpContext.Current.Server.MapPath();
System.Web.HttpContext.Current.Request.PhysicalApplicationPath
在.cs文件中可以用。但是HttpContext.Current.Server.MapPath();這個(gè)獲取的是文件的路徑而不是根目錄。
只有System.Web.HttpContext.Current.Request.PhysicalApplicationPath 這個(gè)才是獲取的根目錄,在寫(xiě)獲取數(shù)據(jù)庫(kù)路徑是應(yīng)該用這個(gè),其他的都有問(wèn)題。
System.Web.HttpContext.Current.Request.PhysicalApplicationPath
和Server.MapPath("~/")效果是一樣的。
Server.MapPath("~/");//無(wú)論代碼所在的文件的、頁(yè)面路勁是什么,永遠(yuǎn)返回 C:\Inetpub\wwwroot\EnglishClub\(就是當(dāng)前程序運(yùn)行的所在根目錄)
如果存儲(chǔ) 附件的路勁 進(jìn)數(shù)據(jù)庫(kù)的話,不應(yīng)該把絕對(duì)路勁存進(jìn)去。應(yīng)該只存儲(chǔ) 文件名部分。例如:
/uploads/abc.txt
當(dāng)需要瀏覽文件的時(shí)候,在在讀取出來(lái)的路徑:(即/uploads/abc.txt),前面+網(wǎng)站的路勁:例如:
http://abc.com+"/uploads/abc.txt"
補(bǔ)充:
ASP.NET中獲取網(wǎng)站根目錄和物理路徑完整實(shí)例:
/// <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 站點(diǎn) 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 站點(diǎn) 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, @"\\$", RegexOptions.Compiled).Success) AppPath = AppPath.Substring(0, AppPath.Length - 1); } return AppPath; }
看完上述內(nèi)容,你們掌握怎么在ASP.NET中獲取網(wǎng)站的根目錄的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。