濮阳杆衣贸易有限公司

主頁 > 知識(shí)庫 > ASP.NET編程獲取網(wǎng)站根目錄方法小結(jié)

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

熱門標(biāo)簽:聊城智能電銷機(jī)器人電話 西寧電銷外呼系統(tǒng)公司 海東防封電銷卡 安陸市地圖標(biāo)注app 上海市三維地圖標(biāo)注 南昌自動(dòng)外呼系統(tǒng)線路 寧德防封版電銷卡 云南外呼系統(tǒng)代理 辦公用地圖標(biāo)注網(wǎng)點(diǎn)怎么操作

本文實(shí)例講述了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();這個(gè)獲取的是文件的路徑而不是根目錄。

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

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

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

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

/uploads/abc.txt
當(dāng)需要瀏覽文件的時(shí)候,在在讀取出來的路徑:(即/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;
}

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

您可能感興趣的文章:
  • 入侵ASP.net網(wǎng)站的經(jīng)驗(yàn)附利用代碼
  • asp.net網(wǎng)站安全從小做起與防范小結(jié)
  • ASP.NET 網(wǎng)站開發(fā)中常用到的廣告效果代碼
  • asp.net網(wǎng)站的404錯(cuò)誤頁面的正確設(shè)置方法
  • asp.net B2B網(wǎng)站對(duì)接支付寶接口
  • ASP.net(C#)從其他網(wǎng)站抓取內(nèi)容并截取有用信息的實(shí)現(xiàn)代碼
  • ASP.NET網(wǎng)站使用Kindeditor富文本編輯器配置步驟
  • asp.net獲取網(wǎng)站目錄物理路徑示例
  • iis6網(wǎng)站屬性沒有asp.net選項(xiàng)卡的解決辦法
  • ASP.NET網(wǎng)站實(shí)時(shí)顯示時(shí)間的方法
  • VS2010發(fā)布Web網(wǎng)站技術(shù)攻略
  • ASP.NET MVC5網(wǎng)站開發(fā)用戶登錄、注銷(五)
  • vs2010制作簡(jiǎn)單的asp.net網(wǎng)站
  • VS2010制作第一個(gè)簡(jiǎn)單網(wǎng)站

標(biāo)簽:南寧 汕尾 贛州 衢州 崇左 洛陽 青海

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ASP.NET編程獲取網(wǎng)站根目錄方法小結(jié)》,本文關(guān)鍵詞  ASP.NET,編程,獲取,網(wǎng)站,根,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ASP.NET編程獲取網(wǎng)站根目錄方法小結(jié)》相關(guān)的同類信息!
  • 本頁收集關(guān)于ASP.NET編程獲取網(wǎng)站根目錄方法小結(jié)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    宣威市| 台中县| 巴林右旗| 蒲城县| 阿拉善盟| 浮山县| 常州市| 南昌县| 金阳县| 韶关市| 高碑店市| 盐亭县| 朝阳市| 师宗县| 新密市| 仪陇县| 陇川县| 清涧县| 浦城县| 福州市| 两当县| 濮阳市| 兴隆县| 射洪县| 岑溪市| 香格里拉县| 莱阳市| 莒南县| 沙河市| 正镶白旗| 桦甸市| 玛曲县| 青川县| 利辛县| 梅河口市| 渭源县| 遂溪县| 广平县| 蕉岭县| 广元市| 府谷县|