- ASP.Net Cookies Overview
- Adding parameters to query string at run time
- Reflection Binding Flags Enumeration
- .Net reflector
SEO is becoming more and more important issue these days, and your site root url can lose its SEO rank if there are too many different query string attached such as http://www.yoursite.com/?a=b&b=c and http://www.yoursite.com/default.aspx?wt=129. It will be nice you can keep the site root clean lik http://www.yoursite.com
My company employed Scrum from this year and in March sprint, the above was one of the user stories I worked on.
The implementation logic is like this
- If the url has any query string, store query string values into cookie and redirect it to site root without query string.
- If there is no query string, then check if cookie has any query string. If the cookie exists, then manually populate Request.QueryString object with the values from cookie.
One tricky bit is Request.QueryString is read only property. So you need to find a private value that the property uses inside. I could do that using .Net reflector and it is _queryString. Using Reflection, you can set the value of a private variable.
The followings are the codes I wrote for this. Of course, I could not write this without the help from the above links.
protected override void OnInit(EventArgs e)
if (Request.QueryString.Count > 0)
private void RedirectToCleanSiteRootUrl()
HttpCookie cookie = new HttpCookie(“QS”);
foreach (string key in Request.QueryString.AllKeys)
cookie.Values[key] = Request.QueryString[key];
cookie.Expires = DateTime.Now.AddDays(1d);
private void PopulateQueryStringFromCookie()
NameValueCollection collection = (NameValueCollection) Request.GetType().GetField(“_queryString”,
BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
PropertyInfo readOnlyInfo = collection.GetType().GetProperty(“IsReadOnly”,
BindingFlags.NonPublic | BindingFlags.Instance);
Response.Cookies[“QS”].Expires = DateTime.Now.AddDays(-1d);