我的web api中有一个post方法,它返回一个字符串,我从客户端调用该方法.我如何获得返回的值.
发布方法
public String Post(Models.SQNotificationDataAccessRepository.NotificationEntry notificationEntry)
{
String externalReferenceID = String.Empty;
if (notificationEntry == null)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
externalReferenceID= dbTransactionLayer.PopulateEsiTable(notification);
return externalReferenceID;
}
Run Code Online (Sandbox Code Playgroud)
客户
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:12819/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Notification notification = new Notification()
{
id = 102,
To = new String[] { "jamesBond@yahoo.com", "cmsds@email.com" },
Title = "Notification WebService Client Test",
MessageBody = "The message body will go there",
DelieveryType = "Email",
Response = true
};
HttpResponseMessage response = await client.PostAsJsonAsync("api/NotificationEntry/Post",notification);
var result = response;
Console.WriteLine("Successfully delivered:" + result.ToString);
}
Run Code Online (Sandbox Code Playgroud)
PostAsJsonAsync返回一个HttpResponseMessage,该Content属性具有应包含响应主体的属性.所以你可能会这样做:
var response = await client.PostAsJsonAsync("api/NotificationEntry/Post",notification);
var result = await response.Content.ReadAsStringAsync();
Console.WriteLine("Successfully delivered:" + result);
Run Code Online (Sandbox Code Playgroud)
然后result应该包含您正在寻找的价值.如果string返回的内容不是其他内容,result则会包含该内容的序列化版本.所以它可以被解析/反序列化/等.如所须.