Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove query string parameters from the url?

Assume I have the link http://www.somesite.com/file.aspx?a=1&b=2

And now I want to remove all the parameters, so it becomes:

http://www.somesite.com/file.aspx

Or I may want to remove only 1 of the parameters such as

http://www.somesite.com/file.aspx?b=2

Is there a way to do the above in C#? What is happening is that I am coming from another page with a parameter called edit in the url, but when the page does a post back, the edit parameter is still there, so it still thinks it is in edit mode. Example:

User A goes to page one.aspx and clicks on an edit link. They are taken to two.aspx?edit=true. During page load, it sees the the query string parameter edit is not null and it puts the contents in edit mode. Once the user is done editing, the page is refreshed, but the url is still two.aspx?edit=true and keeps the contents in edit mode, when in fact it should be two.aspx

like image 437
Xaisoft Avatar asked Dec 01 '22 06:12

Xaisoft


2 Answers

Request.Querystring is read-only collection - You cannot modify that.

If you need to remove or change the param in querystring only way out is to trigger a new GET request with updated querystring - This means you will have to do Response.Redirect with updated URL. This will cause you lose the viewstate of the current page.

like image 59
this. __curious_geek Avatar answered Dec 03 '22 18:12

this. __curious_geek


Use the PostBackUrl property, for example:

<asp:Button ID="DoneEditingButton" runat="server" Text="Done editing" PostBackUrl="~/two.aspx" />
like image 43
jannagy02 Avatar answered Dec 03 '22 18:12

jannagy02