C# 获取Http Post方法返回的值
我的web api中有一个post方法,它返回一个字符串,我正在从客户端调用该方法。如何获取正在返回的值 发布方法C# 获取Http Post方法返回的值,c#,asp.net-mvc,C#,Asp.net Mvc,我的web api中有一个post方法,它返回一个字符串,我正在从客户端调用该方法。如何获取正在返回的值 发布方法 public String Post(Models.SQNotificationDataAccessRepository.NotificationEntry notificationEntry) { String externalReferenceID = String.Empty; if (notificationEntry ==
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;
}
客户端
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);
}
PostAsJsonAsync
返回一个HttpResponseMessage
,该消息具有一个内容
属性,该属性应包含响应正文。所以你可以这样做:
var response = await client.PostAsJsonAsync("api/NotificationEntry/Post",notification);
var result = await response.Content.ReadAsStringAsync();
Console.WriteLine("Successfully delivered:" + result);
然后,
result
应该包含您要查找的值。如果返回的不是字符串
,则结果
将具有该内容的序列化版本。因此,可以根据需要对其进行分析/反序列化等。result.ToString()有什么问题?该输出是什么,您想要什么?它返回大量内容,但我关心的是,externalRefId。我找不到它。