[英]Unsupported Media Type error when using array as parameter in web api?
我遇到了我在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调用此终结点。 我也尝试在客户端标头中设置content-type,例如
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添加到请求头实际上会绕过此问题(在邮递员中)。
可能您的Accept标头与415无关。请确保您正确地形成了url。
您也可以指定[FromUri]进行参数设置,并像api?ids[]=1&ids[]=2
一样构建您的uri。 看到这个答案 。
或者,如果您想进一步控制uri的形成方式,请查看以下答案。
问题是[ArrayInput(“ ids”,Separator =',')]及其与WebApi 5.2.7不兼容。
传递数组参数时:应将其连接为JSON格式的字符串,然后在将数组参数值传递给WebAPI接口之前,调用jQuery将字符串转换为JSON。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.