Search code examples
c#request.form

Can I modify Request.Form variables?


I try Request.Form.Set(k, v) but it's throwing exception

Collection is read-only


Solution

  • This is exactly the same as modifying Request.Querystring. Both are internally complicated by private properties and what could be deemed a bug, however there are two possible solutions I'm aware of (I'll dismiss the response.redirect plan out of hand - that's terrible).

    Method one is to use reflection to modify the collection directly:

    NameValueCollection oQuery = Request.QueryString;
    oQuery = (NameValueCollection)Request.GetType().GetField("_queryString",BindingFlags.NonPublic | BindingFlags.Instance).GetValue(Request);
    PropertyInfo oReadable = oQuery .GetType().GetProperty("IsReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);
    oReadable.SetValue(oQuery, false, null);
    oQuery["foo"] = "bar";
    oReadable.SetValue(oQuery, true, null); 
    

    Plan B, which I think lends itself better to unit testing is to avoid dealing with the collection directly and instead pass it as a NameValueCollection to any method you want to handle it, shallow copying whatever you need out of it. I've used this myself to mock web requests.

    Edit: Marc Gravell gave more eloquent reasons for plan B