Numan
Numan

Reputation: 3948

ASP.NET MVC: Remove unused/not supported query string values

How do I clear/remove query string parameters, which my MVC action, doesn't require/support?

For instance, my action requires, say an id and a bool flag, so the url would be something like: http://localhost:someport/controller/action/?id=1&remove=true

But, if a user types in something like, http://localhost:someport/controller/action/?id=1&remove=true&some-junk-param=0

Then, I want the some-junk-param to be removed and not shown in the address bar, when the request is processed.

Any thoughts?

Upvotes: 0

Views: 524

Answers (1)

Anton Sutarmin
Anton Sutarmin

Reputation: 847

If you need to get rid of unwanted query string parameters, you have two general options:

  1. Do it on server-side. You can achive this only with redirection, that means when browser asks URL with bad query string, server redirects browser to URL with good query string.

Caveats:

  • In this case we have redundant query just for cleaning query string.
  • User will have trash in browser history.

  1. Do it on client-side. ASP.NET MVC Model binder will get only expected parameters from query string, so it's nothing bad with having other values in query string. You can check your URL on client-side with javascript and rewrite it with or without changing history using History API (IE10+).

Caveats:

  • In this case you will have to support consistency about allowed parameters between JS and C# code

Of course every way is suitable for it's own cases, but looking at caveats the second way is better, because it affects developer expirience whereas first way affects user expirience.

Upvotes: 1

Related Questions