我的 web api 中有以下方法
public class AccessPanelController : ApiController
{
public HttpResponseMessage PostGrantAccess([FromUri]GrantAccessRequest grantAccessRequest)
{
var deviceId = grantAccessRequest.DeviceId;
var grantAccessResponse = new GrantAccessResponse()
{
Status = "OK"
};
var response = Request.CreateResponse<GrantAccessResponse>(HttpStatusCode.OK, grantAccessResponse);
return response;
}
}
GrantAccessRequest 类:
public class GrantAccessRequest
{
public string DeviceId { get; set; }
}
客户:
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:55208/");
// Add an Accept header for JSON format.
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
Uri uri = null;
var request = new GrantAccessRequest { DeviceId = "bla" };
var response = client.PostAsJsonAsync("api/accesspanel", uri).Result;
if (response.IsSuccessStatusCode)
{
uri = response.Headers.Location;
}
在我的PostGrantAccess
方法中,grantAccessRequest
是构造的,但DeviceId
为空