将多种内容类型发布到Web API

编程入门 行业动态 更新时间:2024-10-25 08:23:17
本文介绍了将多种内容类型发布到Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个Web API,我想发布一个图像文件+一些数据,以便在服务器上收到它时正确处理它。

I have a web api and I would like to post an image file + some data in order to process it correctly when it is received at the server.

调用代码如下所示:

using(var client = new HttpClient()) using(var content = new MultipartFormDataContent()) { client.BaseAddress = new Uri("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方法签名如下:

And the web api method signature looks like this:

public async Task<HttpResponseMessage> Post([FromBody]FeedItemParams parameters)

当我运行它时,我得到一个 UnsupportedMediaType 异常。我知道这与 ObjectContent 有关,因为这种方法在我仅在 ID 中传递时有效。查询字符串而不是正文中的对象。

When I run this I get an UnsupportedMediaType exception. I know this has something to do with the ObjectContent, since this method worked when I was passing just an ID in the query string instead of the object in the body.

有什么想法我在这里出错吗?

Any ideas where I'm going wrong here?

推荐答案

WebAPI内置格式化程序仅支持以下媒体类型: application / json , text / json ,应用程序/ xml ,文本/ xml 和应用程序/ x- www-form-urlencoded

WebAPI built-in formatters only support the following media types: application/json, text/json, application/xml, text/xml and application/x-www-form-urlencoded

对于 multipart / form-data 您正在发送,请查看发送HTML表单数据和 ASP.NET WebApi:MultipartDataMediaFormatter

For multipart/form-data, which is what you are sending, take a look at Sending HTML Form Data and ASP.NET WebApi: MultipartDataMediaFormatter

示例客户端

using (var client = new HttpClient()) { using (var content = new MultipartFormDataContent()) { client.BaseAddress = new Uri("localhost:54711/"); content.Add(new StreamContent(File.OpenRead(@"d:\foo.jpg")), "foo", "foo.jpg"); var parameters = new FeedItemParams() { Id = "1234", Comment = "Some comment about this or that." }; content.Add(new ObjectContent<FeedItemParams>(parameters, new JsonMediaTypeFormatter()), "parameters"); var result = client.PostAsync("/api/Values", content).Result; } }

如果遵循第一篇文章,则为示例控制器

Sample controller if you follow the first article

public async Task<HttpResponseMessage> PostFormData() { // Check if the request contains multipart/form-data. if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("~/App_Data"); var provider = new MultipartFormDataStreamProvider(root); // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); //use provider.FileData to get the file //use provider.FormData to get FeedItemParams. you have to deserialize the JSON yourself return Request.CreateResponse(HttpStatusCode.OK); }

更多推荐

将多种内容类型发布到Web API

本文发布于:2023-10-12 01:23:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1483358.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多种   类型   内容   API   Web

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!