zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    钱多,人傻,快来快来
    Rabbitmq的使用及Web监控工具使用
    Fiddler的配置
    哪个微信编辑器比较好用?
    js手机号批量滚动抽奖代码实现
    Webform和MVC,为什么MVC更好一些?
    自学MVC看这里——全网最全ASP.NET MVC 教程汇总
    客如云系统访谈
    Asp.Net MVC2.0 Url 路由入门---实例篇
    架设自己的FTP服务器 Serv-U详细配置图文教程
  • 原文地址:https://www.cnblogs.com/kevinGao/p/2323339.html
Copyright © 2011-2022 走看看