0

I have the following method in my WebAPI 2 project.

public class TestController : ApiController
{
    [HttpPost]
    public void Test(HttpRequestMessage request)
    {
        var content = request.Content;
        string jsonContent = content.ReadAsStringAsync().Result;
    }
}

My custom object looks like this;

public class Test
{
    public int Id { get; set; }
    public string Name { get; set; }
}

If I post some sample data like

<Test>
    <Id>12345</Id>
    <Name>My Name</Name>
</Test>

The resulting value in jsonContent is correct. My question is how best should I serialise the HttpRequestMessage (content) into my object Test so that I can perform additional validation / tasks etc.

Should I pass HttpRequestMessage into the method or is it possible to pass something like

public void Test(Test oTest)
2
  • Just to make sure i understand what you're asking, you want to know how to deserialize your jsonContent variable into your Test class? Commented Apr 20, 2014 at 22:37
  • My question is should I be passing HttpRequestMessage as the parameter, or pass the object like Test(Test oTest), is their a best practice for this. Commented Apr 21, 2014 at 12:38

2 Answers 2

6

It sounds like you're asking how to deserialize jsonContent into a new instance of your Test class, as the first comment mentions above. I would suggest looking into Json.NET. Then you could do something like:

public class TestController : ApiController
{
  [HttpPost]
  public void Test(HttpRequestMessage request)
  {
    var content = request.Content;
    string jsonContent = content.ReadAsStringAsync().Result;
    Test test = new Test();
    test = JsonConvert.DeserializeObject<Test>(jsonContent);
    //Do validation stuff...
  }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a parameter in your action method like this.

[HttpPost]
public void Test(Test oTest)

ASP.NET Web API will deserialize the request message body (JSON or XML) into Test. Based on the content type header in the request, web API can handle both JSON and XML content out-of-box. In the case of XML, Web API uses DCS, by default. The XML you have shown in your post will not get deserialized as-is. Try returning a Test object and see how it gets serialized by web API and use the same XML in your POST request for binding to work correctly.

BTW, if you use the Test parameter in your action method, Web API will consume the request body stream. So, you will not be able to read it inside the action method like what you are doing.

2 Comments

If I use the Test parameter, is this the preferred way, or is it better practice to use HttpRequestMessage and use the content of HttpRequestMessage
There are multiple ways you can do this and you have to choose the way that suits you. Generally, binding to a custom type has advantages. For example, regardless of the content type, web api binds the request body to your parameter. If you manually read the body, you have to manually deserialize it yourself. For most of the scenarios, this is useful. Also, unit testing of action method is comparatively easier than using HttpRequestMessage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.