本文實(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)站