我有一个客户在测试期间向我提供相互矛盾的信息。我不认为他们在撒谎,而是更加困惑。所以,我想在我的 ASP.Net 应用程序中设置一些简单的审计。具体来说,当调用任何页面时,我想立即将查询字符串和/或表单 POST 数据插入到日志表中。只是原始值。
查询字符串很简单。但是似乎没有办法在不使用 BinaryRead 的情况下获取原始形式的 POST 数据,如果我这样做,那么我以后就不会使用 Request.Form 集合了。
有人知道解决这个问题的方法吗?
编辑:tvanfosson 建议使用 Request.Params。我一直在寻找更易于使用的东西(比如 Request.Querystring,仅用于 POST),但我想我可以轻松地遍历所有参数并构建一个 name=value& 等字符串。
最佳答案
您可以创建一个自定义 HttpModule 来捕获对您的应用程序发出的所有请求,这样您就不需要触摸每个页面,并且您可以仅在测试期间使用它,而不会降低生产性能。
一个示例实现是:
public class CustomModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.EndRequest += new EventHandler(context_BeginRequest);
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpContext context = ((HttpApplication)sender).Context;
// you can use the context.Request here to send it to the database or a log file
}
}
您需要将模块添加到您的 web.config
<httpModules>
<add name="CustomModule" type="CustomModule"/>
</httpModules>
关于c# - .Net 表单 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/194759/