我正在编写一个web api,它有一个post方法接受从UI上传的文件.
public async Task<List<string>> PostAsync()
{
if (Request.Content.IsMimeMultipartContent("form-data"))
{
string uploadPath = HttpContext.Current.Server.MapPath("~/uploads");
var streamProvider = new MyStreamProvider(uploadPath);
await Request.Content.ReadAsMultipartAsync(streamProvider);
return streamProvider.FileData
.Select(file => new FileInfo(file.LocalFileName))
.Select(fi => "File uploaded as " + fi.FullName + " (" + fi.Length + " bytes)")
.ToList();
}
else
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.BadRequest, "Invalid Request!");
throw new HttpResponseException(response);
}
}
Run Code Online (Sandbox Code Playgroud)
然后我邮递员要求上述行动.我将content-type标头设置为multipart/form-data,但在执行操作期间发生错误.这是错误信息正文:
"提供了无效的'HttpContent'实例.它没有带'border'参数的'multipart'内容类型标头.\ r \nParameter name:content"
我去了邮差标题,但我发现请求标题内容类型in设置为application-json.

谁能帮我?
将Microsoft.Aspnet.Webapi.Webhost包安装到我的web api项目后,我最终得到以下异常:
无法加载文件或程序集'System.Web.Http,Version = 5.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.定位的程序集的清单定义与程序集引用不匹配.(HRESULT异常:0x80131040)
我遇到Web Api 2的问题.我在ContactGroupsController web api中编写了这3个动作.
// POST api/contactgroups
[System.Web.Http.HttpPost]
public IEnumerable<ContactsGroup> All()
{
return _biz.GetAllContactsGroup();
}
// POST api/contactgroups/5
[System.Web.Http.HttpPost]
public ContactsGroup FindById(int id)
{
return _biz.GetContactGroup(id);
}
// POST api/contactgroups
public HttpResponseMessage CreateContactGroup(ContactsGroup item)
{
item.UserId = HttpContext.Current.User.Identity.GetUserId();
item.DateAdded = DateTime.Now;
if (!ModelState.IsValid)
return Request.CreateResponse(HttpStatusCode.NotAcceptable, item);
if (!_biz.CreateContactGroup(item)) return Request.CreateResponse(HttpStatusCode.NotFound, item);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, item);
response.Headers.Location = new Uri(Request.RequestUri + item.Id.ToString());
return response;
}
Run Code Online (Sandbox Code Playgroud)
当我想将http post方法发送到"/ api/contactgroups"时,会出现以下错误:
{
"$id": "1",
"Message": "An error has occurred.",
"ExceptionMessage": "Multiple actions …Run Code Online (Sandbox Code Playgroud)