asp.net-web-api – 当请求具有不受支持的Content-Type时,如何配
发布时间:2020-12-16 06:22:25 所属栏目:asp.Net 来源:网络整理
导读:如果向我的Web API服务发出请求,该服务的Content-Type标头包含该服务不支持的类型,则会返回500 Internal Server Error状态代码,其中包含类似于以下内容的消息: {"Message":"An error has occurred.","ExceptionMessage":"No MediaTypeFormatter is availabl
如果向我的Web API服务发出请求,该服务的Content-Type标头包含该服务不支持的类型,则会返回500 Internal Server Error状态代码,其中包含类似于以下内容的消息:
{"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,IFormatterLogger formatterLogger) at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request,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)"} 我宁愿按照建议返回415 Unsupported Media Type状态代码,例如here. 如何配置我的服务来执行此操作? 解决方法
这是我提出的解决这个问题的方法.
它广泛地基于描述的here,用于在没有可接受的响应内容类型时发送406 Not Acceptable状态代码. 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); } } 在设置服务配置时: config.MessageHandlers.Add(new UnsupportedMediaTypeConnegHandler()); 请注意,这要求char集也匹配.您可以通过仅检查标头的MediaType属性来放宽此限制. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 从信号器js客户端到集线器功能传递连接令牌是安
- asp-classic – 经典的ASP gremlims,每当使用HTML特殊字符时
- “ASP.Net身份”如何与“Windows身份基金会”进行比较?
- asp.net-mvc – 在表单提交时传递整个模型
- asp.net – 如何阻止MonoDevelop在调试时打开Safari窗口?
- asp.net-web-api2 – 创建新的IHttpActionResult动作结果方
- asp.net-mvc – EntityFramework – 连接字符串在哪里?
- asp.net-mvc – .net Mvc 3 Ajax.BeginForm,获取表单元素
- asp.net-mvc – NLog,Elmah共享库
- asp.net-mvc – MVC4上的服务堆栈
推荐文章
站长推荐
- 如何从ASP.NET中的母版页访问内容页面控件
- 我应该如何组织我的ASP.Net主题和常见的CSS文件
- ASP.NET优化不会将LESS转换为CSS
- asp.net – RequiredFieldValidator – 如何摆脱
- asp.net-mvc-3 – ASP.NET MVC – 导航当前页面突
- asp.net – WCF:是否有一个属性要在OperationCo
- asp.net – Server.Transfer throws执行子请求时
- asp.net – 脚本控件不是注册的脚本控件
- asp.net+ajaxfileupload.js 实现文件异步上传代码
- Buddy Class中的必需属性不适用于Entity Framewo
热点阅读