如何从一个URL主要有效地除去查询字符串? [英] How to efficiently remove a query string by Key from a Url?

查看:160
本文介绍了如何从一个URL主要有效地除去查询字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从一个网址取消导航键的查询字符串?

我有下面的方法,它工作正常,但只是想知道有没有更好/更短的方式吗?或者一个内置的.NET方法可以做到这一点更有效?

 公共静态字符串RemoveQueryStringByKey(字符串URL,字符串键)
        {
            变种indexOfQuestionMark = url.IndexOf(?);
            如果(indexOfQuestionMark == -1)
            {
                返回URL;
            }

            VAR的结果= url.Substring(0,indexOfQuestionMark);
            变种查询字符串= url.Substring(indexOfQuestionMark + 1);
            VAR queryStringParts = queryStrings.Split(新[] {'和;'});
            VAR isFirstAdded = FALSE;

            对于(INT指数= 0;指数< queryStringParts.Length;指数++)
            {
                VAR的keyValue = queryStringParts [指数] .Split(新的char [] {'='});
                如果(keyValue的[0] ==键)
                {
                    继续;
                }

                如果(!isFirstAdded)
                {
                    结果+ =?;
                    isFirstAdded = TRUE;
                }
                其他
                {
                    结果+ =与&;
                }

                结果+ = queryStringParts [指数]
            }

            返回结果;
        }
 

例如,我可以这样调用它:

  Console.WriteLine(RemoveQueryStringByKey(@"http://www.domain.com/uk_pa/PostDetail.aspx?hello=hi&xpid=4578", Xpid中));
 

希望这个问题是清楚的。

谢谢

解决方案

这工作得很好:

 公共静态字符串RemoveQueryStringByKey(字符串URL,字符串键)
        {
            VAR URI =新的URI(URL);

            //这个得到所有的查询字符串键值对作为一个集合
            VAR newQueryString = HttpUtility.ParseQueryString(uri.Query);

            //这个删除键是否存在
            newQueryString.Remove(键);

            //这个获取页面的路径从根没有查询字符串
            字符串pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path);

            返回newQueryString.Count> 0
                ?的String.Format({0} {1},pagePathWithoutQueryString,newQueryString)
                :pagePathWithoutQueryString;
        }
 

一个例子:

<$p$p><$c$c>RemoveQueryStringByKey("https://www.google.co.uk/#hl=en&output=search&sclient=psy-ab&q=cookie", Q);

和回报:

  https://www.google.co.uk/#hl=en&output=search&sclient=psy-ab
 

How to remove a query string by Key from a Url?

I have the below method which works fine but just wondering is there any better/shorter way? or a built-in .NET method which can do it more efficiently?

 public static string RemoveQueryStringByKey(string url, string key)
        {
            var indexOfQuestionMark = url.IndexOf("?");
            if (indexOfQuestionMark == -1)
            {
                return url;
            }

            var result = url.Substring(0, indexOfQuestionMark);
            var queryStrings = url.Substring(indexOfQuestionMark + 1);
            var queryStringParts = queryStrings.Split(new [] {'&'});
            var isFirstAdded = false;

            for (int index = 0; index <queryStringParts.Length; index++)
            {
                var keyValue = queryStringParts[index].Split(new char[] { '=' });
                if (keyValue[0] == key)
                {
                    continue;
                }

                if (!isFirstAdded)
                {
                    result += "?";
                    isFirstAdded = true;
                }
                else
                {
                    result += "&";
                }

                result += queryStringParts[index];
            }

            return result;
        }

For example I can call it like:

  Console.WriteLine(RemoveQueryStringByKey(@"http://www.domain.com/uk_pa/PostDetail.aspx?hello=hi&xpid=4578", "xpid"));

Hope the question is clear.

Thanks,

解决方案

This works well:

public static string RemoveQueryStringByKey(string url, string key)
        {                   
            var uri = new Uri(url);

            // this gets all the query string key value pairs as a collection
            var newQueryString = HttpUtility.ParseQueryString(uri.Query);

            // this removes the key if exists
            newQueryString.Remove(key);

            // this gets the page path from root without QueryString
            string pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path);

            return newQueryString.Count > 0
                ? String.Format("{0}?{1}", pagePathWithoutQueryString, newQueryString)
                : pagePathWithoutQueryString;
        }

an example:

RemoveQueryStringByKey("https://www.google.co.uk/#hl=en&output=search&sclient=psy-ab&q=cookie", "q");

and returns:

https://www.google.co.uk/#hl=en&output=search&sclient=psy-ab

这篇关于如何从一个URL主要有效地除去查询字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆