1

I have following querystring & i want to remove or update part of querystring

?language=en-us&issue=5&pageid=18&search=hawaii&search=hawaii // this is what i need to format

Problem is i have duplicate querystring for search key.

I tried following code but it is not working

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
        ? "{0}?{1}".FormatWith(pagePathWithoutQueryString, newQueryString)
        : pagePathWithoutQueryString;
}

FormatWith generates error

string' does not contain a definition for 'FormatWith' and no extension method 'FormatWith' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?

WORKING CODE:

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

Function Call

var URL = Request.Url.AbsoluteUri;
var uri = new Uri(Helper.RemoveQueryStringByKey(URL, "search"));

In my actual logic i have to check if QueryString search is present if it is then i remove it and replace it with the new search keyword.

You can apply it as per logic, & the first example code which was not working was due to FormatWith which i was not able to fix so i used solution provide by Oded which works for me.

Learning
  • 19,469
  • 39
  • 180
  • 373

2 Answers2

4

Use string.Format directly. It looks like you copied the code from a code base that defines a FormatWith extension method which you have not copied (or haven't included the namespace it lives in).

Using string.Format:

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

Another option, if the extension method is in the codebase, is to add a using declaration with the namespace the extension method is in.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
  • You are right i took this code from http://stackoverflow.com/questions/11052744/how-to-efficiently-remove-a-query-string-by-key-from-a-url i am not able to find a complete example on this – Learning Feb 24 '13 at 13:03
  • @KnowledgeSeeker - I had given you a different bit of code that _should_ work just fine. – Oded Feb 24 '13 at 13:04
  • I tried first code it generate error `The format of the URI could not be determined` i will try update one.Just to be more clear i dont pass all the URI, I only need to pass the query string part only as i have show in example – Learning Feb 24 '13 at 13:13
  • @KnowledgeSeeker - Time to crack open the debugger. See what the values you are trying to concatenate are. – Oded Feb 24 '13 at 13:15
  • This is how my part of code problem is with URI conversion `QueryString = Request.Url.Query.ToString(); Helper.RemoveQueryStringByKey(QueryString,"search"); Helper.RemoveQueryStringByKey("?language=en-us&issue=5&pageid=18&search=hawaii", "search")` – Learning Feb 24 '13 at 13:20
  • I fixed it rather than passing QueryString i am passing now complete URL as function breaks URL into URI.Query.. – Learning Feb 24 '13 at 13:25
0
return newQueryString.Count > 0
        ? "{0}?{1}".FormatWith(pagePathWithoutQueryString, newQueryString)
        : pagePathWithoutQueryString;

string' does not contain a definition for 'FormatWith' and no extension method 'FormatWith' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?

I believe that the error is self-explainatory, there's an error in your return-code.
There isn't a FormatWith in the string-object.


QueryStrings can also be collected with Request.QueryString, which will return a collection of keys and values. Altering this collection and then resending it with Response.QueryString might do what you want.

BlueCacti
  • 9,729
  • 3
  • 20
  • 24