4

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

7 Answers 7

13

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That's what I actually was doing, so I guess I am stuck with that. Thanks.
If you use sessions, you have no problem
2

Use the PostBackUrl property, for example:

<asp:Button ID="DoneEditingButton" runat="server" Text="Done editing" PostBackUrl="~/two.aspx" />

Comments

2

Try something like this.

if (url.Contains("?"))
            url = url.Substring(0, url.IndexOf("?"));

In this example, I'm checking if the url even contains a query string, and if it does, subtracting getting the "left part" of the string prior to the ?.

2 Comments

Welcome to Stack Overflow! Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.
again, urls can be encoded and '?' becomes '%3F'. this manipulation should be done using Uri class or similar
1

When you are done with the edit you are doing a post back so just define the action to post to two.aspx instead of just posting back to itself that way it will drop off the get parameters.

Comments

1

How about checking Page.IsPostBack to see if the current request is a postback or not?

Comments

1

if you have only string, you can use:

strinULR.Split('?').First();

or

strinULR.Split('?').FirstOrDefault();

1 Comment

urls can be encoded and '?' becomes '%3F'. this manipulation should be done using Uri class or similar
0

Late but you can do this to remove query string from URL without another GET Request. http://www.codeproject.com/Tips/177679/Removing-Deleting-Querystring-in-ASP-NET

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.