c# - .Net 表单 POST

标签 c# asp.net form-post

我有一个客户在测试期间向我提供相互矛盾的信息。我不认为他们在撒谎,而是更加困惑。所以,我想在我的 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/

相关文章:

javascript - Asp.Net MVC 4 从表单发布中的对象数组自动绑定(bind)模型

c# - 如何使用 Lucene 对 Sitecore 中具有多个位置的文档进行地理空间搜索?

c# - 空的 LongListSelector 导致无限的 ScrollViewer

c# - asp.net 在 c# 中使用 switch case for datetime.now.hour 格式

c# - 将选定值的集合从 Select2-Multi DropDownList 传递到 Controller

javascript - 使用 javascript 中的表单发布将字符串作为文件上传

json - 从 Flash AS3 发送 Json 表单

c# - 如何在 C# 中一次跳出多个循环?

c# - asp.net中global.asax的作用是什么

javascript - 打字时的 Asp.net 数字分组