Learning
Learning

Reputation: 20001

How to remove or update part of query string in asp.net

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.

Upvotes: 1

Views: 1569

Answers (2)

BlueCacti
BlueCacti

Reputation: 10820

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.

Upvotes: 0

Oded
Oded

Reputation: 498924

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.

Upvotes: 4

Related Questions