user290043
user290043

Reputation:

ASP.NET WebApi How to get at the JSON the Controller received

In my controller:

public class ProfileController : ApiController
{
    public HttpResponseMessage PostCreateProfile (HubBasicProfile bp)
    {

        HttpResponseMessage result = null;    
        if (bp == null)
        {
            result = Request.CreateResponse(HttpStatusCode.InternalServerError, "Could not instantiate Profile with JSON provided.");
        }


...

The automatic deserialization of the json to instantiating a bp object happens in the background.

I want to have a look at the json that that the controller receives to try to resolve an issue that we are experiencing. At this time, bp is always null. (And when I say I want to have a look, I want to inspect it and see where the deserializer is having issues.)

I've looked at the json being sent and that seems to be fine.

So, can anybody tell me how to debug this json -> bp object process?

Upvotes: 5

Views: 3601

Answers (2)

Mark Jones
Mark Jones

Reputation: 12184

Enable tracing and see if the serialiser is logging any warnings http://www.hanselman.com/blog/WhenInDoubtTurnOnTracing.aspx.

Specifically Look here for tracing:

Failing that look here to implement your own very basic one.

Test run your JSON through JsonLint.

Also ensure your Model is correctly defined using something like Json2C#.

Also add a breakpoint and check the ModelState property in your controller this may contain some binding errors and warnings.

Upvotes: 6

MarkG
MarkG

Reputation: 1869

It should be available in the form collection on the request if you wanted to view it in the debugger

Upvotes: 0

Related Questions