2

Is there an easy way to dump a complete HttpRequest. I need this to analyze a problem in my web application. The HttpRequest contains many properties, and i don´t want to write it on my own.

 HttpRequest request = HttpContext.Current.Request;
 request.Dump();

I´m looking for a method like one in Linqpad that prints out all properties with values.

Jehof
  • 34,674
  • 10
  • 123
  • 155
  • 1
    have a look at Sean's post [here](http://stackoverflow.com/questions/852181/c-printing-all-properties-of-an-object) – Luke94 Nov 09 '12 at 14:51
  • 1
    Do you need to see the properties of the HttpRequest object, or do you need to inspect the HTTP Request that gets sent? If the latter is the case, a web debugging tool like [Fiddler](http://www.fiddler2.com/fiddler2/version.asp) might prove itself useful. – CodeCaster Nov 09 '12 at 14:56
  • @CodeCaster: I need to analyze and inspect the HttpRequest on server side. So Fiddler won´t work here. – Jehof Nov 09 '12 at 15:05

2 Answers2

2

The built in SaveAs() method might help you out.

http://msdn.microsoft.com/en-us/library/system.web.httprequest.saveas.aspx

It's description is:

Saving the request context to disk can be useful in debugging.

Jesse Webb
  • 43,135
  • 27
  • 106
  • 143
2

You can write an extension method like this:

var dict = someObj.DumpProperties();
var dumpStr = String.Join("\n", 
                dict.Select(kv => kv.Key + "=" + kv.Value ?? kv.Value.ToString()));

.

public static class MyExtensions
{
    public static Dictionary<string, object> DumpProperties(this object obj)
    {
        var props = obj.GetType()
            .GetProperties()
            .ToDictionary(p => p.Name, p => p.GetValue(obj, null));

        return props;
    }
}
L.B
  • 114,136
  • 19
  • 178
  • 224