最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2015年11月17日 10:04:01   作者:一狼哥  
這篇文章主要介紹了ASP.NET編程獲取網(wǎng)站根目錄方法,較為詳細(xì)的分析了ASP.NET針對網(wǎng)站目錄及物理路徑的操作技巧,并給出了實(shí)例予以總結(jié),需要的朋友可以參考下

本文實(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)行的所在根目錄)

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

/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;
}

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

相關(guān)文章

最新評論

行唐县| 罗城| 武邑县| 高邮市| 内乡县| 西城区| 和顺县| 梅河口市| 泸州市| 玉溪市| 锡林浩特市| 基隆市| 怀宁县| 渝北区| 青海省| 磐石市| 循化| 珲春市| 石楼县| 平谷区| 古蔺县| 蚌埠市| 丹东市| 昌吉市| 林芝县| 西林县| 南江县| 兴文县| 常宁市| 车致| 普格县| 赤壁市| 光泽县| 绥中县| 开远市| 应城市| 寿阳县| 沐川县| 芦山县| 阿拉善右旗| 彝良县|