.Net 表单 POST

发布于 2024-07-06 17:45:57 字数 406 浏览 10 评论 0原文

我有一个客户在测试过程中向我提供了相互矛盾的信息。 我不认为他们在撒谎,而是更加困惑。 因此,我想在 ASP.Net 应用程序中设置一些简单的审核。 具体来说,当调用任何页面时,我想立即将查询字符串和/或表单 POST 数据插入日志表中。 只是原始值。

查询字符串很简单。 但似乎没有办法在不使用 BinaryRead 的情况下获取原始表单 POST 数据,如果我这样做,那么我以后就会放弃使用 Request.Form 集合。

有谁知道解决这个问题的方法吗?

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

I've got a client that, during testing, is giving me conflicting information. I don't think they are lying but more confused. So, I would like to setup some simple auditing in my ASP.Net application. Specifically, right when any page is called, I want to immediately insert the Querystring and/or form POST data into a log table. Just the raw values.

Querystring is easy. But there doesn't seem to be a way to get the raw form POST'ed data without using BinaryRead, and if I do that, then I screw myself out of using the Request.Form collection later on.

Does anyone know a way around this?

EDIT: tvanfosson suggested Request.Params. I was looking for something that was easier to use (like Request.Querystring, only for POST), but I guess I could just as easily loop through all params and build a string of name=value&, etc).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷乱花海 2024-07-13 17:45:57

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

You can create a custom HttpModule to capture all request made to your application so you don't need to touch every page and you can use it only during testing just not to slow down performance in production.

A sample implementation would be:

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
    }
}

You need to add the module to your web.config

<httpModules>
    <add name="CustomModule" type="CustomModule"/>
</httpModules>
真心难拥有 2024-07-13 17:45:57

所有表单数据应位于 Request.Params< /a>. 您需要在每个页面上执行此操作,或者可能使用 HttpModule。

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

All of the form data should be in Request.Params. You'd need to do this on every page, though or maybe use an HttpModule.

[EDIT] If you want to get the form parameters separately use Request.Form, along with Request.QueryString

情丝乱 2024-07-13 17:45:57

我建议针对这种类型的场景实现 HttpHandler 或 HttpModule。 您可以从 Page_Load 事件获取 POST 数据,但在这里实现此日志记录功能并不那么可维护。

I would recommend implementing and HttpHandler or an HttpModule for this type of scenario. You can get to the POST Data from the Page_Load event but implementing this logging facility here is not as maintainable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文