java
sql
html
php
iphone
c
xml
database
xcode
android
objective-c
eclipse
silverlight
flash
json
perl
algorithm
oracle
tsql
delphi
I think you approached the problem correctly.
This is the code that worked for me:
public void OnAuthorization(AuthorizationContext filterContext) { if(filterContext.HttpContext.Request.QueryString["some_key"] != null) { RedirectToCleanUrl(filterContext); } } private void RedirectToCleanUrl(AuthorizationContext filterContext) { var alteredQueryStringPairs = filterContext.HttpContext.Request.QueryString .Cast<KeyValuePair<string, object>>() .Where(pair => pair.Key != "some_key"); var routeValueDictionary = new RouteValueDictionary( filterContext.RouteData.Values.Union(alteredQueryStringPairs)); filterContext.Result = new RedirectToRouteResult(routeValueDictionary); }
I used the answers from these questions to clean up your initial code:
how do i access query string params in asp.net mvc?
Make NameValueCollection accessible to LINQ Query
Here's the code I ended up writing:
protected void StripQueryStringAndRedirect(System.Web.HttpContextBase httpContext, string[] keysToRemove) { var queryString = new NameValueCollection(httpContext.Request.QueryString); foreach (var key in keysToRemove) { queryString.Remove(key); } var newQueryString = ""; for (var i = 0; i < queryString.Count; i++) { if (i > 0) newQueryString += "&"; newQueryString += queryString.GetKey(i) + "=" + queryString[i]; } var newPath = httpContext.Request.Path + (!String.IsNullOrEmpty(newQueryString) ? "?" + newQueryString : String.Empty); if (httpContext.Request.Url.PathAndQuery != newPath) { httpContext.Response.Redirect(newPath, true); } }
You might also want to UrlEncode the query string params, but I'll leave this up to you.