39

如何通过键从 URL 中删除查询字符串?

我有以下方法可以正常工作,但只是想知道有没有更好/更短的方法?还是可以更有效地执行此操作的内置 .NET 方法?

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

例如,我可以这样称呼它:

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

希望问题很清楚。

谢谢,

4

15 回答 15

109

这很好用:

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

一个例子:

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

并返回:

https://www.google.co.uk/search?#hl=en&output=search&sclient=psy-ab
于 2012-06-18T09:30:51.020 回答
6
    var queryString = "hello=hi&xpid=4578";
    var qs = System.Web.HttpUtility.ParseQueryString(queryString);
    qs.Remove("xpid");
    var newQuerystring = qs.ToString();

这仍然适用于 .NET 5。

于 2012-06-15T14:53:19.610 回答
5

在 System 命名空间中有一个名为UriBuilder的有用类。我们可以将它与几个扩展方法一起使用来执行以下操作:

Uri u = new Uri("http://example.com?key1=value1&key2=value2");
u = u.DropQueryItem("key1");

或者像这样:

Uri u = new Uri("http://example.com?key1=value1&key2=value2");
UriBuilder b = new UriBuilder(u);
b.RemoveQueryItem("key1");
u = b.Uri;

扩展方法:

using System;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;

public static class UriExtensions
{
    public static Uri DropQueryItem(this Uri u, string key)
    {
        UriBuilder b = new UriBuilder(u);
        b.RemoveQueryItem(key);
        return b.Uri;
    }
}
public static class UriBuilderExtensions
{
    private static string _ParseQueryPattern = @"(?<key>[^&=]+)={0,1}(?<value>[^&]*)";
    private static Regex _ParseQueryRegex = null;

    private static Regex ParseQueryRegex
    {
        get
        {
            if (_ParseQueryRegex == null)
            {
                _ParseQueryRegex = new Regex(_ParseQueryPattern, RegexOptions.Compiled | RegexOptions.Singleline);
            }
            return _ParseQueryRegex;

        }
    }

    public static void SetQueryItem(this UriBuilder b, string key, string value)
    {
        NameValueCollection parms = ParseQueryString(b.Query);
        parms[key] = value;
        b.Query = RenderQuery(parms);
    }

    public static void RemoveQueryItem(this UriBuilder b, string key)
    {
        NameValueCollection parms = ParseQueryString(b.Query);
        parms.Remove(key);
        b.Query = RenderQuery(parms);
    }       
    private static string RenderQuery(NameValueCollection parms)
    {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<parms.Count; i++)
        {
            string key = parms.Keys[i];
            sb.Append(key + "=" + parms[key]);
            if (i < parms.Count - 1)
            {
                sb.Append("&");
            }
        }
        return sb.ToString();
    }
    public static NameValueCollection ParseQueryString(string query, bool caseSensitive = true)
    {
        NameValueCollection pairs = new NameValueCollection(caseSensitive ? StringComparer.Ordinal : StringComparer.OrdinalIgnoreCase);

        string q = query.Trim().TrimStart(new char[] {'?'});
        MatchCollection matches = ParseQueryRegex.Matches(q);

        foreach (Match m in matches)
        {
            string key = m.Groups["key"].Value;
            string value = m.Groups["value"].Value;
            if (pairs[key] != null)
            {
                pairs[key] = pairs[key] + "," + value;
            }
            else
            {
                pairs[key] = value;
            }

        }

        return pairs;

    }

}
于 2018-06-19T17:14:32.973 回答
3

我们也可以使用正则表达式

string queryString = "Default.aspx?Agent=10&Language=2"; //Request.QueryString.ToString();
string parameterToRemove="Language";   //parameter which we want to remove
string regex=string.Format("(&{0}=[^&\s]+|(?<=\?){0}=[^&\s]+&?)",parameterToRemove);   //this will not work for javascript, for javascript you can do following
string finalQS = Regex.Replace(queryString, regex, "");

//javascript(following is not js syntex, just want to give idea how we can able do it in js)
string regex1 = string.Format("(&{0}=[^&\s]+)",parameterToRemove);
string regex2 = string.Format("(\?{0}=[^&\s]+&?)",parameterToRemove);
string finalQS = Regex.Replace(queryString, regex1, "").Replace(queryString, regex2, "");

https://regexr.com/3i9vj

于 2017-12-20T13:51:37.400 回答
2

我知道这是一个相当古老的问题,但我读到的所有内容都感觉有点复杂。

public Uri GetUriWithoutQueryParam( Uri originalUri, string paramKey ) {
  NameValueCollection newQuery = HttpUtility.ParseQueryString( originalUri.Query );
  newQuery.Remove( paramKey );
  return new UriBuilder( originalUri ) { Query = newQuery.ToString() }.Uri;
}
于 2021-02-03T21:46:32.140 回答
1

这个怎么样:

        string RemoveQueryStringByKey(string url, string key)
    {
        string ret = string.Empty;

        int index = url.IndexOf(key);
        if (index > -1)
        {
            string post = string.Empty;

            // Find end of key's value
            int endIndex = url.IndexOf('&', index);
            if (endIndex != -1) // Last query string value?
            {
                post = url.Substring(endIndex, url.Length - endIndex);
            }

            // Decrement for ? or & character
            --index;
            ret = url.Substring(0, index) + post;
        }

        return ret;
    }
于 2012-06-15T14:56:51.310 回答
1

我找到了一种不使用正则表达式的方法:

private string RemoveQueryStringByKey(string sURL, string sKey) {
    string sOutput = string.Empty;

    int iQuestion = sURL.IndexOf('?');
    if (iQuestion == -1) return (sURL);

    int iKey = sURL.Substring(iQuestion).IndexOf(sKey) + iQuestion;
    if (iKey == -1) return (sURL);

    int iNextAnd = sURL.Substring(iKey).IndexOf('&') + iKey + 1;

    if (iNextAnd == -1) {
        sOutput = sURL.Substring(0, iKey - 1);
    }
    else {
        sOutput = sURL.Remove(iKey, iNextAnd - iKey);
    }

    return (sOutput);
}

我确实尝试过在最后添加另一个字段,它也可以正常工作。

于 2012-06-15T15:01:15.310 回答
0

我在想最短的方法(我相信在所有情况下都会产生一个有效的 URL,假设 URL 开始是有效的)是使用这个正则表达式(getRidOf你试图删除的变量名在哪里)并且替换是一个零长度字符串""):

(?<=[?&])getRidOf=[^&]*(&|$)

甚至可能

\bgetRidOf=[^&]*(&|$)

虽然可能不是绝对最漂亮的URL,但我认为它们都是有效的:

         INPUT                                         OUTPUT
      -----------                                   ------------
blah.com/blah.php?getRidOf=d.co&blah=foo        blah.com/blah.php?blah=foo
blah.com/blah.php?f=0&getRidOf=d.co&blah=foo    blah.com/blah.php?f=0&blah=foo
blah.com/blah.php?hello=true&getRidOf=d.co      blah.com/blah.php?hello=true&
blah.com/blah.php?getRidOf=d.co                 blah.com/blah.php?

这是一个简单的正则表达式替换:

Dim RegexObj as Regex = New Regex("(?<=[?&])getRidOf=[^&]*(&|$)")
RegexObj.Replace("source.url.com/find.htm?replace=true&getRidOf=PLEASE!!!", "")

...应该导致字符串:

"source.url.com/find.htm?replace=true&"

...这似乎对 ASP.Net 应用程序有效,而replace等于true(不是true&或类似的东西)

如果您遇到无法使用的情况,我会尝试对其进行调整:)

于 2012-06-15T20:36:02.823 回答
0

在删除您的QueryString.

 PropertyInfo isreadonly = 
          typeof(System.Collections.Specialized.NameValueCollection).GetProperty(
          "IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
        // make collection editable
        isreadonly.SetValue(this.Request.QueryString, false, null);
        // remove
        this.Request.QueryString.Remove("yourKey");
于 2014-04-22T11:31:06.883 回答
0
public static string RemoveQueryStringByKey(string sURL, string sKey)
    {
        string sOutput = string.Empty;
        string sToReplace = string.Empty;

        int iFindTheKey = sURL.IndexOf(sKey);
        if (iFindTheKey == -1) return (sURL);

        int iQuestion = sURL.IndexOf('?');
        if (iQuestion == -1) return (sURL);

        string sEverythingBehindQ = sURL.Substring(iQuestion);
        List<string> everythingBehindQ = new List<string>(sEverythingBehindQ.Split('&'));
        foreach (string OneParamPair in everythingBehindQ)
        {
            int iIsKeyInThisParamPair = OneParamPair.IndexOf(sKey);
            if (iIsKeyInThisParamPair != -1)
            {
                sToReplace = "&" + OneParamPair;
            }
        }

        sOutput = sURL.Replace(sToReplace, "");
        return (sOutput);
    }
于 2014-08-25T15:13:19.270 回答
0

抱歉,这有点脏,但应该可以在较旧的框架中使用

public String RemoveQueryString( String rawUrl  , String keyName)
{
    var currentURL_Split =  rawUrl.Split('&').ToList();
    currentURL_Split = currentURL_Split.Where(o => !o.ToLower().StartsWith(keyName.ToLower()+"=")).ToList();
    String New_RemovedKey = String.Join("&", currentURL_Split.ToArray()); 
    New_RemovedKey = New_RemovedKey.Replace("&&", "&");
    return New_RemovedKey;
}
于 2017-02-16T09:38:03.847 回答
0

这是我的解决方案:

我添加了一些额外的输入验证。

public static void TryRemoveQueryStringByKey(ref string url, string key)
{
    if (string.IsNullOrEmpty(url) ||
        string.IsNullOrEmpty(key) ||
        Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute) == false)
    {
        return false;
    }            

    try
    {
        Uri uri = new Uri(url);

        // This gets all the query string key value pairs as a collection
        NameValueCollection queryCollection = HttpUtility.ParseQueryString(uri.Query);
        string keyValue = queryCollection.Get(key);

        if (url.IndexOf("&" + key + "=" + keyValue, StringComparison.OrdinalIgnoreCase) >= 0)
        {
            url = url.Replace("&" + key + "=" + keyValue, String.Empty);
            return true;
        }
        else if (url.IndexOf("?" + key + "=" + keyValue, StringComparison.OrdinalIgnoreCase) >= 0)
        {
            url = url.Replace("?" + key + "=" + keyValue, String.Empty);
            return true;
        }
        else
        {
            return false;
        }
    }
    catch
    {
        return false;
    }
}

一些单元测试示例:

string url1 = "http://www.gmail.com?a=1&cookie=cookieValue"
Assert.IsTrue(TryRemoveQueryStringByKey(ref url1,"cookie")); //OUTPUT: "http://www.gmail.com?a=1"

string url2 = "http://www.gmail.com?cookie=cookieValue"  
Assert.IsTrue(TryRemoveQueryStringByKey(ref url2,"cookie")); //OUTPUT: "http://www.gmail.com"

string url3 = "http://www.gmail.com?cookie="  
Assert.IsTrue(TryRemoveQueryStringByKey(ref url2,"cookie")); //OUTPUT: "http://www.gmail.com"
于 2017-08-16T06:56:14.230 回答
0

这是一个完整的解决方案,适用于 >= 0 指定的参数和任何形式的 URL:

    /// <summary>
    /// Given a URL in any format, return URL with specified query string param removed if it exists
    /// </summary>
    public static string StripQueryStringParam(string url, string paramToRemove)
    {
        return StripQueryStringParams(url, new List<string> {paramToRemove});
    }

    /// <summary>
    /// Given a URL in any format, return URL with specified query string params removed if it exists
    /// </summary>
    public static string StripQueryStringParams(string url, List<string> paramsToRemove)
    {
        if (paramsToRemove == null || !paramsToRemove.Any()) return url;

        var splitUrl = url.Split('?');
        if (splitUrl.Length == 1) return url;

        var urlFirstPart = splitUrl[0];
        var urlSecondPart = splitUrl[1];

        // Even though in most cases # isn't available to context,
        // we may be passing it in explicitly for helper urls
        var secondPartSplit = urlSecondPart.Split('#');
        var querystring = secondPartSplit[0];
        var hashUrlPart = string.Empty;
        if (secondPartSplit.Length > 1)
        {
            hashUrlPart = "#" + secondPartSplit[1];
        }
        var nvc = HttpUtility.ParseQueryString(querystring);
        if (!nvc.HasKeys()) return url;

        // Remove any matches
        foreach (var key in nvc.AllKeys)
        {
            if (paramsToRemove.Contains(key))
            {
                nvc.Remove(key);
            }
        }

        if (!nvc.HasKeys()) return urlFirstPart;
        return urlFirstPart + 
               "?" + string.Join("&", nvc.AllKeys.Select(c => c.ToString() + "=" + nvc[c.ToString()])) + 
               hashUrlPart;
    }
于 2018-10-02T09:53:34.680 回答
0

这个老问题的更现代的答案,以防其他人像我一样偶然发现它。

这是使用 Uri 类来解析 URL(如果您的 URL 已经在 Uri 对象中,则可以跳过)和 LINQ 来过滤查询字符串。

public static string RemoveQueryStringByKey(string url, string key)
{
    var uri = new Uri(url, UriKind.Absolute);
    var queryParts = uri.Query
        .TrimStart('?')
        .Split('&')
        .Where(item => string.CompareOrdinal(item, key) != 0);
    return uri.Scheme + Uri.SchemeDelimiter
        + uri.Authority
        + uri.AbsolutePath
        + "?" + string.Join("&", queryParts);
}
于 2020-11-21T23:30:56.700 回答
-1
string url = HttpContext.Current.Request.Url.AbsoluteUri;
string[] separateURL = url.Split('?');

NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(separateURL[1]);
queryString.Remove("param_toremove");

string revisedurl = separateURL[0] + "?" + queryString.ToString();
于 2016-09-14T13:16:59.583 回答