OWASP O2 Platform Blog

Making HttpRequest QueryString and Form Editable

If you find yourself with the need to modify the values of the HttpRequest QueryString or Forms (lets say if you are inside an HttpModule) you will find that they can’t be changed because they have a property called IsReadOnly that is set to true.

To change this behaviour you can use reflection.

Here is the normal wasy to do it: (sample from this online thread)

NameValueCollection QS = Request.QueryString;
QS = (NameValueCollection)Request.GetType()
                                 .GetField("_queryString", BindingFlags.NonPublic | BindingFlags.Instance)
PropertyInfo readOnlyInfo = QS.GetType()
                              .GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
readOnlyInfo.SetValue(QS, false, null);
QS["parametername"] = "parameterValue";

or you can just do it in one lines using O2’s powerful reflection APIs

Request.QueryString.property("IsReadOnly", false);          // make the queryString readWrite

Request.QueryString["parametername"] = "parameterValue";    // edit an existing parameter
Request.QueryString.Add("test","1234");"                    // add a new parameter

March 25, 2011 - Posted by | .NET

1 Comment »

  1. […] Making HttpRequest QueryString and Form Editable […]

    Pingback by Script to fetch and present large number of Wordpress.com blog entries « O2Platform.com for Developers | April 16, 2011 | Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: