无法配置内容类型的多部分的Web API [英] unable to configure Web API for content type multipart

查看:172
本文介绍了无法配置内容类型的多部分的Web API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Web API的工作 - 网页API 2.我的基本需要是创造一个API更新用户的配置文件。在此iOS和Android会送我的multipart / form-data的请求。他们会送我一些参数与图像。但每当我试图创建API,我的模型来空每次都可以。

I am working on Web API's - Web API 2. My basic need is to create an API to update profile of the user. In this the ios and android will send me the request in multipart/form-data. They will send me few parameters with an image. But whenever i try to create the API, my model comes to be null every time.

我在WebApiConfig也增加了这一行:

I have added this line in WebApiConfig also:

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("multipart/form-data"));

这是我的类:

public class UpdateProfileModel
{
   public HttpPostedFileBase ProfileImage { get; set; }
   public string Name { get; set; }
}

这是我的控制器:

[Route("api/Account/UpdateProfile")]
[HttpPost]
public HttpResponseMessage UpdateProfile(UpdateProfileModel model)
{
}

我连在我的模型得到的参数值。难道我做错了什么?

I am even not getting parameter values in my Model. Am i doing something wrong ?

有关这一问题的答案没有一个是对我有帮助。其对第3天,我已经试过几乎所有的东西和每一个方法。但我无法去实现它。

None of the answers related to this were helpful for me. Its about 3rd day and i have tried almost everything and every method. but i am unable to achieve it.

虽然我可以用这个但这如下图所示,但这似乎并没有成为一个不错的办法。所以我避免它。

Although i can use this but this as shown below but this doesn't seem to be a good approach. so i am avoiding it..

var httpRequest = HttpContext.Current.Request;
if (httpRequest.Form["ParameterName"] != null)
{
    var parameterName = httpRequest.Form["ParameterName"];
}

和的文件,我可以做到这一点:

and for files i can do this:

if (httpRequest.Files.Count > 0)
{
     //i can access my files here and save them
}

如果您有什么好的办法为它还是请解释一下我为什么我不能在模型得到这个值请帮助。

Please help if you have any good approach for it Or Please explain me why i am unable to get this values in Model.

感谢很多提前

推荐答案

默认情况下没有建成,可以多部分处理/表单数据并执行模型绑定API的媒体类型格式。内置的媒体类型格式化是:

By default there is not a media type formatter built into the api that can handle multipart/form-data and perform model binding. The built in media type formatters are :

 JsonMediaTypeFormatter: application/json, text/json
 XmlMediaTypeFormatter: application/xml, text/xml
 FormUrlEncodedMediaTypeFormatter: application/x-www-form-urlencoded
 JQueryMvcFormUrlEncodedFormatter: application/x-www-form-urlencoded

这就是为什么大多数的答案涉及接管负责直接从控制器内部的请求,读取数据的原因。但是,Web API 2格式集合,就是要为开发的起点和并不意味着是所有实现的解决方案。有已创建以创建MediaFormatter将处理多部分形式的数据的其他解决方案。一旦MediaTypeFormatter类已创建它可以横跨网页API的多个实现重新使用。

This is the reason why most answers involve taking over responsibility to read the data directly from the request inside the controller. However, the Web API 2 formatter collection is meant to be a starting point for developers and not meant to be the solution for all implementations. There are other solutions that have been created to create a MediaFormatter that will handle multipart form data. Once a MediaTypeFormatter class has been created it can be re-used across multiple implementations of Web API.

<一个href=\"http://stackoverflow.com/questions/17924655/how-create-a-multipartformformatter-for-asp-net-4-5-web-api\">How创建MultipartFormFormatter为ASP.NET 4.5的Web API

您可以下载并构建全面实施网络API源2 code,并看到媒体格式化的默认实现本身不处理多部分的数据。
HTTPS://aspnetwebstack.$c$cplex.com/

You can download and build the full implementation of the web api 2 source code and see that the default implementations of media formatters do not natively process multi part data. https://aspnetwebstack.codeplex.com/

这篇关于无法配置内容类型的多部分的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆