How to Remove Item from Querystring in ASP.NET Using C#

How can I remove item from querystring in asp.net using c#?

I answered a similar question a while ago. Basically, the best way would be to use the class HttpValueCollection, which the QueryString property actually is, unfortunately it is internal in the .NET framework.
You could use Reflector to grab it (and place it into your Utils class). This way you could manipulate the query string like a NameValueCollection, but with all the url encoding/decoding issues taken care for you.

HttpValueCollection extends NameValueCollection, and has a constructor that takes an encoded query string (ampersands and question marks included), and it overrides a ToString() method to later rebuild the query string from the underlying collection.

how to remove query string in asp.net

Its not a rule to remove the query string, but its KIND OF A REDIRECT (url rewriting), see.

if (this.Request.Path.Contains("/search.aspx"))
base.Context.RewritePath("/details.aspx?id=100");

You set this code at Global.asax Application_BeginRequest method.

Of course, instead of the Contains method you better use a regex.

This code means that you will reuse details.aspx but using search.aspx on your URL. So instead of redirecting user to /details.aspx?id=100 you will directly send him to /search.aspx and its done, you dont have to "remove the query string" as theres no query string to the user.

How do I remove items from the query string for redirection?

You'd have to reconstruct the url and then redirect. Something like this:

string url = Request.RawUrl;

NameValueCollection params = Request.QueryString;
for (int i=0; i<params.Count; i++)
{
if (params[i].GetKey(i).ToLower() == "foo")
{
url += string.Concat((i==0 ? "?" : "&"), params[i].GetKey(i), "=", params.Get(i));
}
}
Response.Redirect(url);

Anyway, I didn't test that or anything, but it should work (or at least get you in thye right direction)

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

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/search?#hl=en&output=search&sclient=psy-ab&q=cookie", "q");

and returns:

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

Removing a querystring from url in asp.net

You have several options:

1) In your code behind, just set the LinkButton's URL to the shorter address if the querystring contains a "UserID" key:

if (Request.QueryString["UserID"] != null) {
this.LinkButton.PostBackUrl = "http://UserProfileManager.com";
} else {
// other address
}

2) Send the UserID in a hidden field instead of the querystring.

3) Separate your view and edit pages - putting everything in one *.aspx is probably going to cause more trouble than it's worth down the road.

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

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.

I would like to clear my querystring after using it on page load c#

You can't just change the URL in the address bar of a browser. You could redirect the browser to the URL without the query string, but seeing how you are using the value to populate controls on the page being render that would mean you would need to still need to have that value.

When you say "it lingers after submitting the update", do you mean the user chooses the currency and is redirected to the the page with this query string? If so could you change this action to a POST instead of a GET? Then you could put the currencyId in the body of the POST. If you can't switch it to a POST, then there are a few ideas I listed below.

If you are using session, you could store the currencyId in the user's session. But that would only make sense if you needed to use this value on other requests; as using session is a big decision and if you can keep your website stateless you should.

With that being said, there are two viable options to keep your site stateless. If you need this value on future requests, you can store it in a cookie. If you only need it on this request, you could have the page do post to the URL without the query string but with the value in the POST body.



Related Topics



Leave a reply



Submit