最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

ASP.NET编程获取网站根目录方法小结

来源:动视网 责编:小采 时间:2020-11-27 22:38:05
文档

ASP.NET编程获取网站根目录方法小结

ASP.NET编程获取网站根目录方法小结:本文实例讲述了ASP.NET编程获取网站根目录方法。分享给大家供大家参考,具体如下: 获取网站根目录的方法有几种如: Server.MapPath(Request.ServerVariables[PATH_INFO]) Server.MapPath(/) Server.MapPath()//当前代
推荐度:
导读ASP.NET编程获取网站根目录方法小结:本文实例讲述了ASP.NET编程获取网站根目录方法。分享给大家供大家参考,具体如下: 获取网站根目录的方法有几种如: Server.MapPath(Request.ServerVariables[PATH_INFO]) Server.MapPath(/) Server.MapPath()//当前代


本文实例讲述了ASP.NET编程获取网站根目录方法。分享给大家供大家参考,具体如下:

获取网站根目录的方法有几种如:

Server.MapPath(Request.ServerVariables["PATH_INFO"])
Server.MapPath("/")
Server.MapPath("")//当前代码文件所在的目录路劲
Server.MapPath(".")
Server.MapPath("../")
Server.MapPath("..") 
Page.Request.ApplicationPath

以上的代码在http://localhost/EnglishClub/manage/WebForm1.aspx页面

运行结果:

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 这个才是获取的根目录,在写获取数据库路径是应该用这个,其他的都有问题。

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

Server.MapPath("~/");//无论代码所在的文件的、页面路劲是什么,永远返回 C:\Inetpub\wwwroot\EnglishClub\(就是当前程序运行的所在根目录)

如果存储 附件的路劲 进数据库的话,不应该把绝对路劲存进去。应该只存储 文件名部分。例如:

/uploads/abc.txt
当需要浏览文件的时候,在在读取出来的路径:(即/uploads/abc.txt),前面+网站的路劲:例如:

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

补充:

ASP.NET中获取网站根目录和物理路径完整实例:

/// <summary>
/// 取得网站的根目录的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 站点 
 AppPath = UrlAuthority;
 else
 //安装在虚拟子目录下 
 AppPath = UrlAuthority + Req.ApplicationPath;
 }
 return AppPath;
}
/// <summary>
/// 取得网站的根目录的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 站点 
 AppPath = UrlAuthority;
 else
 //安装在虚拟子目录下 
 AppPath = UrlAuthority + Req.ApplicationPath;
 }
 return AppPath;
}
/// <summary>
/// 取得网站根目录的物理路径
/// </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程序设计有所帮助。

文档

ASP.NET编程获取网站根目录方法小结

ASP.NET编程获取网站根目录方法小结:本文实例讲述了ASP.NET编程获取网站根目录方法。分享给大家供大家参考,具体如下: 获取网站根目录的方法有几种如: Server.MapPath(Request.ServerVariables[PATH_INFO]) Server.MapPath(/) Server.MapPath()//当前代
推荐度:
标签: 网站 方法 获取
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top