从ASP Web Api IHttpActionResult反序列化byte [] [英] Deserialize byte[] from ASP Web Api IHttpActionResult
本文介绍了从ASP Web Api IHttpActionResult反序列化byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试制定正确的&反序列化来自返回byte []的Asp.Net Web Api方法的响应的最佳方法.
I am trying work out the correct & best way to deserialize the response from a Asp.Net Web Api method that returns byte[].
Web Api方法如下所示
The Web Api method looks like this
public IHttpActionResult Get()
{
byte[] content = GetContent();
return Ok(content);
}
我正在呼叫端点
string content;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
HttpResponseMessage response = await client.GetAsync("api/v1/thecorrectpath");
if (response.IsSuccessStatusCode)
{
content = await response.Content.ReadAsStringAsync();
}
}
当我将响应读入 content
时,其格式如下:
When I read the response into content
it is in the format below
<base64Binary xmlns="http://schemas.microsoft.com/2003/10/Serialization/">SfSEjEyNzE9MNgMCD2a8i0xLjcNJeLjzC...R4Cg==</base64Binary>
将这种响应转换为 byte []
的最佳实践是什么?
What would be a best practice way to convert this response into a byte[]
?
推荐答案
我会使用 json.net 为此.
Web API:
public string Get()
{
byte[] content = GetContent();
var data = JsonConvert.SerializeObject(content);
return data;
}
客户:
private static async Task GetData()
{
string content;
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:23306/");
client.DefaultRequestHeaders.Accept.Clear();
HttpResponseMessage response = await client.GetAsync("home/get");
if (response.IsSuccessStatusCode)
{
content = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<byte[]>(content);
}
}
}
这篇关于从ASP Web Api IHttpActionResult反序列化byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文