Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to clear query string parameters when posting back?

I have a form that sometimes gets linked to with some query string parameters. The problem is that when I post back the form, the query string parameter is still there. It's not really an issue the way I have it setup, but I just don't like it being there, and could see it being a problem if you needed to check for input in a certain order.

Is there a way to clear that query string parameter in an easy, clean way? I know I could change the PostBackURL on the button, but that doesn't seem too efficient.

like image 629
John B Avatar asked May 12 '09 17:05

John B


People also ask

How do you clear a query string?

The only way to 'clear' the query string is to do a redirect to the same URL but without the query string part. Is there particular reason why you want it cleared? If security is the issue, try using a POST call instead or encrypt the query string.

Can we pass query parameter in POST method?

POST should not have query param. You can implement the service to honor the query param, but this is against REST spec. "Why do you pass it in a query param?" Because, like for GET requests, the query parameters are used to refer to an existing resource.

Can I use query string in POST method?

A POST request can include a query string, however normally it doesn't - a standard HTML form with a POST action will not normally include a query string for example.

How do I clear URLSearchParams?

delete() The delete() method of the URLSearchParams interface deletes the given search parameter and all its associated values, from the list of all search parameters.


2 Answers

put this at the bottom of your page?

<script language="javascript" type="text/javascript">
    document.forms[0].action = window.location.pathname;
</script>
like image 140
Al W Avatar answered Oct 27 '22 00:10

Al W


I was stuck with same issue.

I solved it using the following code block:

private void ClearQueryString()
{
    PropertyInfo isreadonly = typeof(System.Collections.Specialized.NameValueCollection).GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);
    isreadonly.SetValue(this.Request.QueryString, false, null);
    this.Request.QueryString.Remove("Action");
    this.Request.QueryString.Remove("param1");
    this.Request.QueryString.Remove("paramN");
}
like image 20
bgS Avatar answered Oct 26 '22 22:10

bgS