Asp.net web api 当请求的内容类型不受支持时,如何配置ASP.NET Web API服务返回的状态代码?
如果请求my Web API服务的Asp.net web api 当请求的内容类型不受支持时,如何配置ASP.NET Web API服务返回的状态代码?,asp.net-web-api,content-negotiation,Asp.net Web Api,Content Negotiation,如果请求my Web API服务的内容类型标头包含该服务不支持的类型,它将返回500内部服务器错误状态代码,并显示类似以下消息: {"Message":"An error has occurred.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'MyDto' from content with media type 'application/UnsupportedContent
内容类型
标头包含该服务不支持的类型,它将返回500内部服务器错误
状态代码,并显示类似以下消息:
{"Message":"An error has occurred.","ExceptionMessage":"No MediaTypeFormatter is available to read an object of type 'MyDto' from content with media type 'application/UnsupportedContentType'.","ExceptionType":"System.InvalidOperationException","StackTrace":" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)
at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)
at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken)"}
{“消息”:“出现错误”,“异常消息”:“没有MediaTypeFormatter可用于从媒体类型为“application/UnsupportedContentType.”的内容中读取类型为“MyDto”的对象”,“异常类型”:“System.InvalidOperationException”,“StackTrace”:“at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent内容、类型、IEnumerable`1格式化程序、IFormatterLogger格式化程序记录器)
位于System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent内容,类型,IEnumerable`1格式化程序,IFormatterLogger格式化程序记录器)
位于System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage请求,类型类型,IEnumerable`1格式化程序,IFormatterLogger格式化程序记录器)
位于System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider,HttpActionContext actionContext,CancellationToken CancellationToken)
在System.Web.Http.Controllers.HttpActionBinding.c__DisplayClass1.b__0(HttpParameterBinding parameterBinder)
在System.Linq.Enumerable.WhereSelectArrayInterator`2.MoveNext()中
在System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1枚举器,CancellationToken CancellationToken)“}
我宁愿按照建议返回415不支持的媒体类型
状态代码,例如
如何配置我的服务以实现此目的?没有自动更改状态代码的配置标志。您可以创建一个MessageHandler,它可能会检查“正在发送的响应”,并将状态代码修改为415。以下是我针对此问题提出的解决方案 它大体上基于所描述的当没有可接受的响应内容类型时发送406不可接受的状态代码
public class UnsupportedMediaTypeConnegHandler : DelegatingHandler {
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request,
CancellationToken cancellationToken) {
var contentType = request.Content.Headers.ContentType;
var formatters = request.GetConfiguration().Formatters;
var hasFormetterForContentType = formatters //
.Any(formatter => formatter.SupportedMediaTypes.Contains(contentType));
if (!hasFormetterForContentType) {
return Task<HttpResponseMessage>.Factory //
.StartNew(() => new HttpResponseMessage(HttpStatusCode.UnsupportedMediaType));
}
return base.SendAsync(request, cancellationToken);
}
}
请注意,这要求字符集也匹配。您可以通过只检查标题的
MediaType
属性来放宽此限制。返回状态代码的标准方法是从您的操作返回HttpResponseMessage。您可以将内容包装在HttpResponseMessage对象中,而不是原始内容,然后设置如下状态:
public System.Net.Http.HttpResponseMessage Getresponse()
{
return new System.Net.Http.HttpResponseMessage() { Content = new System.Net.Http.StringContent(done.ToString()), StatusCode = System.Net.HttpStatusCode.Conflict };
}
我采用了此解决方案,但遇到了一个小问题。对于GET请求,代码仍在运行,并且找不到“null”内容类型(或我的应用程序中的“text/plain”)的格式化程序。因此,要修复此问题,我只需检查它是否为GET请求,如果是,则跳过格式化程序检查。。。
public System.Net.Http.HttpResponseMessage Getresponse()
{
return new System.Net.Http.HttpResponseMessage() { Content = new System.Net.Http.StringContent(done.ToString()), StatusCode = System.Net.HttpStatusCode.Conflict };
}