抓取一个 Delete HttpResponseMessage 的 属性
Grab a property of a Delete HttpResponseMessage
我想测试下面我的 API 请求之一的输出。
async Task DeleteNonExistantFoo()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("Http://localhost:43240/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (HttpResponseMessage response = await client.DeleteAsync("api/foos/1"))
{
var responseContent = await response.Content.ReadAsStringAsync();
//Assert.AreEqual(?????, "A Foo with ID of 1 does not exist.");
}
}
}
我想从下面的响应中获取异常消息,当我向 API 发送 DELETE 请求时,它作为响应给出。在 visual studio 中调试时,它不会让我查看响应内容对象,给我错误
"responseContent Cannot obtain value of the local variable or argument because it is not available at this instruction pointer, possibly because it has been optimized away.
"
我需要将其转换为 JSON 对象才能读取吗?
{
"message": "An error has occurred.",
"exceptionMessage": "A Foo with ID of 1 does not exist.",
"exceptionType": "System.Exception",
"stackTrace": "
}
您可以尝试使用 Newtonsoft.Json
的 JsonConvert.DeserializeObject
方法来读取响应 Json 转换为对象并使用它。
创建 class ApiResponeMoedl
public class ApiResponeMoedl
{
public string message { get; set; }
public string exceptionMessage { get; set; }
public string exceptionType { get; set; }
public string stackTrace { get; set; }
}
然后使用 JsonConvert.DeserializeObject<ApiResponeMoedl>
将您的 json 数据反序列化为 ApiResponeMoedl
对象,然后使用对象的 exceptionMessage
属性 您将获得沙漠信息。
async Task DeleteNonExistantRedirect()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("Http://localhost:43240/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (HttpResponseMessage response = await client.DeleteAsync("api/foos/1"))
{
var responseContent = await response.Content.ReadAsStringAsync();
var respOjb = JsonConvert.DeserializeObject<ApiResponeMoedl>(responseContent);
//respOjb.exceptionMessage
}
}
}
我想测试下面我的 API 请求之一的输出。
async Task DeleteNonExistantFoo()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("Http://localhost:43240/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (HttpResponseMessage response = await client.DeleteAsync("api/foos/1"))
{
var responseContent = await response.Content.ReadAsStringAsync();
//Assert.AreEqual(?????, "A Foo with ID of 1 does not exist.");
}
}
}
我想从下面的响应中获取异常消息,当我向 API 发送 DELETE 请求时,它作为响应给出。在 visual studio 中调试时,它不会让我查看响应内容对象,给我错误
"responseContent Cannot obtain value of the local variable or argument because it is not available at this instruction pointer, possibly because it has been optimized away. "
我需要将其转换为 JSON 对象才能读取吗?
{
"message": "An error has occurred.",
"exceptionMessage": "A Foo with ID of 1 does not exist.",
"exceptionType": "System.Exception",
"stackTrace": "
}
您可以尝试使用 Newtonsoft.Json
的 JsonConvert.DeserializeObject
方法来读取响应 Json 转换为对象并使用它。
创建 class ApiResponeMoedl
public class ApiResponeMoedl
{
public string message { get; set; }
public string exceptionMessage { get; set; }
public string exceptionType { get; set; }
public string stackTrace { get; set; }
}
然后使用 JsonConvert.DeserializeObject<ApiResponeMoedl>
将您的 json 数据反序列化为 ApiResponeMoedl
对象,然后使用对象的 exceptionMessage
属性 您将获得沙漠信息。
async Task DeleteNonExistantRedirect()
{
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri("Http://localhost:43240/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
using (HttpResponseMessage response = await client.DeleteAsync("api/foos/1"))
{
var responseContent = await response.Content.ReadAsStringAsync();
var respOjb = JsonConvert.DeserializeObject<ApiResponeMoedl>(responseContent);
//respOjb.exceptionMessage
}
}
}