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

首頁 > 編程 > .NET > 正文

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

2024-07-10 13:29:38
字體:
供稿:網(wǎng)友

這篇文章主要介紹了ASP.NET編程獲取網(wǎng)站根目錄方法,較為詳細(xì)的分析了ASP.NET針對網(wǎng)站目錄及物理路徑的操作技巧,并給出了實例予以總結(jié),需要的朋友可以參考下

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

獲取網(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ù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中訪問,但是如果你在。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("~/");//無論代碼所在的文件的、頁面路勁是什么,永遠(yuǎn)返回 C:/Inetpub/wwwroot/EnglishClub/(就是當(dāng)前程序運(yùn)行的所在根目錄)

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

/uploads/abc.txt

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

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

補(bǔ)充:

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

 

 
  1. /// <summary> 
  2. /// 取得網(wǎng)站的根目錄的URL 
  3. /// </summary> 
  4. /// <returns></returns> 
  5. public static string GetRootURI() 
  6. string AppPath = ""
  7. HttpContext HttpCurrent = HttpContext.Current; 
  8. HttpRequest Req; 
  9. if (HttpCurrent != null
  10. Req = HttpCurrent.Request; 
  11. string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority); 
  12. if (Req.ApplicationPath == null || Req.ApplicationPath == "/"
  13. //直接安裝在 Web 站點  
  14. AppPath = UrlAuthority; 
  15. else 
  16. //安裝在虛擬子目錄下  
  17. AppPath = UrlAuthority + Req.ApplicationPath; 
  18. return AppPath; 
  19. /// <summary> 
  20. /// 取得網(wǎng)站的根目錄的URL 
  21. /// </summary> 
  22. /// <param name="Req"></param> 
  23. /// <returns></returns> 
  24. public static string GetRootURI(HttpRequest Req) 
  25. string AppPath = ""
  26. if(Req != null
  27. string UrlAuthority = Req.Url.GetLeftPart(UriPartial.Authority); 
  28. if (Req.ApplicationPath == null || Req.ApplicationPath == "/"
  29. //直接安裝在 Web 站點  
  30. AppPath = UrlAuthority; 
  31. else 
  32. //安裝在虛擬子目錄下  
  33. AppPath = UrlAuthority + Req.ApplicationPath; 
  34. return AppPath; 
  35. /// <summary> 
  36. /// 取得網(wǎng)站根目錄的物理路徑 
  37. /// </summary> 
  38. /// <returns></returns> 
  39. public static string GetRootPath() 
  40. string AppPath = ""
  41. HttpContext HttpCurrent = HttpContext.Current; 
  42. if (HttpCurrent != null
  43. AppPath = HttpCurrent.Server.MapPath("~"); 
  44. else 
  45. AppPath = AppDomain.CurrentDomain.BaseDirectory; 
  46. if (Regex.Match(AppPath, @"//$", RegexOptions.Compiled).Success) 
  47. AppPath = AppPath.Substring(0, AppPath.Length - 1); 
  48. return AppPath; 

希望本文所述對大家asp.net程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到ASP.NET教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹寨县| 凤山市| 古交市| 常山县| 诸城市| 宣威市| 乌拉特中旗| 昭通市| 响水县| 长海县| 雅江县| 长顺县| 吉木萨尔县| 临海市| 朔州市| 临清市| 灌阳县| 镇江市| 安塞县| 合山市| 咸丰县| 东丽区| 红原县| 临安市| 玉门市| 丰城市| 达州市| 嘉善县| 黄冈市| 沙坪坝区| 张家口市| 瑞昌市| 凌云县| 年辖:市辖区| 金山区| 嘉荫县| 陇西县| 法库县| 日土县| 清远市| 基隆市|