怎么在ASP.NET中获取网站的根目录

发布时间:2021-01-28 10:30:27 作者:Leah
来源:亿速云 阅读:217

怎么在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中获取网站的根目录的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. 网站根目录在哪里?
  2. asp.net mvc 获取网站的BasePath

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net

上一篇:如何在C#项目中获取当前页面的URL地址

下一篇:微信小程序登录前端设计与实现的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》