【发布时间】:2014-03-08 12:50:25
【问题描述】:
我有一个 web api,我想发布一个图像文件 + 一些数据,以便在服务器接收到它时正确处理它。
调用代码如下所示:
using(var client = new HttpClient())
using(var content = new MultipartFormDataContent())
{
client.BaseAddress = new Uri("http://localhost:8080/");
var fileContent = new ByteArrayContent(File.ReadAllBytes(fileName));
fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
{
FileName = "foo.jpg"
};
content.Add(fileContent);
FeedItemParams parameters = new FeedItemParams()
{
Id = "1234",
comment = "Some comment about this or that."
};
content.Add(new ObjectContent<FeedItemParams>(parameters, new JsonMediaTypeFormatter()), "parameters");
content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");
var result = client.PostAsync("/api/ImageServices", content).Result;
Web api 方法签名如下所示:
public async Task<HttpResponseMessage> Post([FromBody]FeedItemParams parameters)
当我运行它时,我得到一个UnsupportedMediaType 异常。我知道这与ObjectContent 有关,因为当我在查询字符串中传递ID 而不是正文中的对象时,此方法有效。
有什么想法我在这里出错了吗?
【问题讨论】:
标签: c# asp.net-web-api content-type