在Web API中使用数组作为参数时出现不受支持的媒体类型错误?

时间:2019-07-16 06:41:36

标签: c# httpclient

我遇到了一个新错误,这是我在Web Api中从未见过的

Response = StatusCode: 415, ReasonPhrase: 'Unsupported Media Type', Version: 1.1, Content: System.Net.Http.ObjectContent`1[[System.Web.Http.HttpError, System.Web.Http, Version=5.2.7.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]], Headers:  {    Content-Type: application/json; charset=utf-8  } Message = Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details. Data = System.Collections.ListDictionaryInternal InnerException =  TargetSite = System.Threading.Tasks.Task`1[System.Object] ReadContentAsync(System.Net.Http.HttpRequestMessage, System.Type, System.Collections.Generic.IEnumerable`1[System.Net.Http.Formatting.MediaTypeFormatter], System.Net.Http.Formatting.IFormatterLogger, System.Threading.CancellationToken) StackTrace =    at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)     at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)     at System.Web.Http.ModelBinding.FormatterParameterBinding.<ExecuteBindingAsyncCore>d__18.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     at System.Web.Http.Controllers.HttpActionBinding.<ExecuteBindingAsyncCore>d__12.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__5.MoveNext()  --- End of stack trace from previous location where exception was thrown ---     at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)     at System.Web.Http.Controllers.ExceptionFilterResult.<ExecuteAsync>d__6.MoveNext() HelpLink =  Source = System.Web.Http HResult = -2146233088

这是控制器内部的动作

[HttpGet]
[Route("show_many")]
[ArrayInput("ids", Separator = ',')]
public async Task<HttpResponseMessage> GetByIds(int[] ids)
{
}

似乎执行未达到ArrayInput或方法GetByIds。我相信int数组会导致此问题作为参数。当我执行基本的邮递员请求时,一切正常。

我正在通过HttpClient从SDK调用此终结点。我也尝试在客户端标头中设置内容类型,例如

client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

但这也没有帮助。

乌里(Uri)看起来像http://example.com/show_many?ids=1,2,3

这是什么问题?

更新

我注意到此错误发生在Microsofot.AspNet.WebApi 5.2.7中,但未发生在5.2.3中。

此外,我注意到client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue(“ application / json”)); 仅创建Accept标头,而不创建Content-Type。 content-type应该是一个响应头,但是不幸的是,仅将content-type添加到请求头实际上会绕过此问题(在邮递员中)。

4 个答案:

答案 0 :(得分:0)

可能您的Accept标头与415无关。请确保您正确地形成了url。

您也可以指定[FromUri]进行参数设置,并像config.authentication_keys = [:login] 一样构建uri。参见this answer

或者,如果您想进一步控制uri的形成方式,请查看以下答案。

https://stackoverflow.com/a/17553324/1083512

答案 1 :(得分:0)

问题是[ArrayInput(“ ids”,Separator =',')] 及其与WebApi 5.2.7不兼容。

答案 2 :(得分:0)

我正在使用 .Net 3.1 并且遇到了同样的问题。

修复是这样的

[HttpGet("foo")]
public IActionResult GetFoos([FromQuery] FooType[] fooTypes)
{
     return Ok(Foo.One);
}

queryStringapi/myControler/foo?fooType=A&fooType=B;

希望这会有所帮助!

答案 3 :(得分:-1)

传递数组参数时:应将其串联为JSON格式的字符串,然后在将数组参数值传递给WebAPI接口之前调用jQuery将字符串转换为JSON