我可以改变,而无需重新张贴岗位价值的价值? [英] Can I change the value of a POST value without re-POSTing?

查看:160
本文介绍了我可以改变,而无需重新张贴岗位价值的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在IIS 6的世界使用ASP.NET 2.0。

This is using ASP.NET 2.0 in an IIS 6 world.

我有一个用户提交发送的数据是通过POST的形式。接收数据的页面做一些简单的验证。如果验证通过,则黑盒code例程运行,基本上读取使用的Request.Form的数据(NameHere)。

I have a user submitting a form that sends the data to be via POST. The page receiving the data does some simple validations. If validation passes, then a black-box code routine runs that basically reads the data using Request.Form("NameHere").

我希望能够改变POST项的值,然后把它放回去的讯息。我没有修改code,读取的Request.Form(NameHere)的能力,所以我的工作围绕的想法是页面的加载事件过程中修改数据。如果我改变了POST项的值,则暗箱code没有被修改。

I'd like to be able to change the value of the POST item and then put it back on the POST. I don't have the ability to modify the code that reads the Request.Form("NameHere"), so my work around idea is to modify the data during the page's load event. If I change the value of the POST item, then the black-box code doesn't have to be modified.

是否有可能改变对HTTP POST?

Is it possible to change the value of an item on the HTTP POST?

有没有人这样做呢?

谢谢!

推荐答案

尽管这是一个有点哈克,有改变POST变量的值的方法。

Even though it is a bit hacky, there is a way to change the value of a POST variable.

我们可以使用反射来标记的Request.Form 收集非只读,将值改为我们想要的东西,回来为只读再次将其标记(这样其他人们无法改变的值)。使用下面的函数:

We can use Reflection to mark the Request.Form collection as non-readonly, change the value to what we want and mark it back as readonly again (so that other people cannot change values). Use the following function:

protected void SetFormValue(string key, string value)
{
  var collection = HttpContext.Current.Request.Form;

  // Get the "IsReadOnly" protected instance property.
  var propInfo = collection.GetType().GetProperty("IsReadOnly", BindingFlags.Instance | BindingFlags.NonPublic);

  // Mark the collection as NOT "IsReadOnly"
  propInfo.SetValue(collection, false, new object[] { });

  // Change the value of the key.
  collection[key] = value;

  // Mark the collection back as "IsReadOnly"     
  propInfo.SetValue(collection, true, new object[] { });
}

我过我的机器上的code和它工作正常。但是,我不能给出任何性能或便携保证。

I have tested the code on my machine and it works fine. However, I cannot give any performance or portability guarantees.

这篇关于我可以改变,而无需重新张贴岗位价值的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆