asp.net获取当前url及来源url

作者:袖梨 2022-06-25

获取的 URL 是完整的 URL,即包含 QueryString 部分

后台用string url = Request.Url.ToString();

获取 域名:

 代码如下 复制代码

string url=HttpContext.Current.Request.Url.Host;
url= www.111com.net

获取 站点名+页面名+参数:

 代码如下 复制代码

string url=Request.RawUrl;
(或 string url=Request.Url.PathAndQuery;)
url= /aaa/bbb.aspx?id=5&name=kelli


 代码如下 复制代码

#region 获取当前网址的URL
        ///


        /// 获取当前网址的URL
        ///

        /// 返回类型
        ///
        public static string weburl(string stype)
        {
            string url = "";
            switch (stype)
            {
                case "1":
                    url = HttpContext.Current.Request.Url.Host; //返回域名
                    break;

                case "2":
                    url = HttpContext.Current.Request.Url.Query; // 返回参数
                    break;
                case "3":
                    url = HttpContext.Current.Request.Url.AbsolutePath; //获取 站点名+页面


                    break;
                //case "4":
                //    url = Request.RawUrl; //获取 站点名+页面名+参数
                //    break;
                //case "5":
                //    url = Request.Url.ToString(); //返回完整url
                //    break;
            }
            return url;
        }

相关文章

精彩推荐