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

  • 相关阅读:
    能帮你找到网页设计灵感的16个网站
    [转]自定义SqlMembershipProvider方法
    C#实现的根据年月日计算星期几的函数
    分享一个我自己写的支持多条件组合查询的分页存储过程
    史上最强的福克斯遥控钥匙失灵解决方案(zt)
    在页面实现数据还原,在终止数据库进程时,报不能用kill来终结自己的进程
    ViewState使用兼谈序列化
    jQuery 的上传图片预览插件
    Asp.net 备份、还原Ms SQLServer及压缩Access数据库
    aspnet_Membership表的意义
  • 原文地址:https://www.cnblogs.com/amylis_chen/p/2816277.html
Copyright © 2011-2022 走看看