我有一个客户在测试期间给我提供了相互矛盾的信息。我不认为他们撒谎但更混乱。所以,我想在我的ASP.Net应用程序中设置一些简单的审计。具体来说,在调用任何页面时,我想立即将Querystring和/或POST数据插入日志表。只是原始值。

Querystring很简单。但似乎没有办法在不使用BinaryRead的情况下获取原始形式的POST数据,如果我这样做,那么我将在后面使用Request.Form集合。

有没有人知道解决这个问题?

编辑:tvanfosson建议Request.Params。我正在寻找更容易使用的东西(比如Request.Querystring,仅用于POST),但我想我可以轻松地遍历所有参数并构建一个name = value <!> amp;等的字符串)。

有帮助吗?

解决方案

所有表单数据都应该在请求中.Params 的。您需要在每个页面上执行此操作,或者使用HttpModule。

[编辑]如果您想单独获取表单参数,请使用 Request.Form ,以及Request.QueryString

其他提示

您可以创建自定义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>

我建议为这种情况实现HttpHandler或HttpModule。您可以从Page_Load事件获取POST数据,但是在此处实现此日志记录工具并不是可维护的。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top