Null Reference
Null Reference

Reputation: 11360

How to reconstruct return type object in web api

Given that I have the following web api method in my controller

    public HttpResponseMessage PostGrantAccess(GrantAccessRequest grantAccessRequest)
    {
        var deviceId = grantAccessRequest.DeviceId;

        var grantAccessResponse = new GrantAccessResponse()
            {
                Status = "OK"
            };
        var response = Request.CreateResponse<GrantAccessResponse>(HttpStatusCode.OK, grantAccessResponse);
        return response;
    }

Client calling code:

using (var client = new HttpClient())
{
    client.BaseAddress = new Uri("http://localhost:55208/");

    var request = new GrantAccessRequest { DeviceId = "bla" };
    var response = client.PostAsJsonAsync("api/accesspanel", request).Result;
    if (response.IsSuccessStatusCode)
    {
        var uri = response.Headers.Location;
    }
}

How do I get back GrantAccessResponse at the client?

Upvotes: 1

Views: 488

Answers (1)

Darrel Miller
Darrel Miller

Reputation: 142252

response.Content.ReadAsAsync<GrantAccessResponse>()

Upvotes: 4

Related Questions