views:

223

answers:

3
+1  Q: 

.Net Form POST

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).

+2  A: 

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

tvanfosson
Matt Dawdy
Request.Form should contain only the form parameters and you already know how to get the query string.
tvanfosson
Yeah, Request.Form I knew about. I was hoping that Request.Form put them nicely and neatly into a string. But, I can loop through all the Request.Form and build a string myself, and then just use Querystring. I guess I was just being lazy. Thanks!
Matt Dawdy
+1  A: 

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.

SaaS Developer
I'm not sure I understand how an HttpHandler or HttpModule would help here. Is there a way, using those, that I can get to the raw form data and not screw up the Request object in the process?
Matt Dawdy
+3  A: 

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>
Eduardo Campañó