我的ASP.NET Web API中有一个 POST
方法,该方法将由无法发送 Content-Type
标头的客户端使用 . 我想检查Request的 Content-Type
标头,如果它为null,我想将它设置为 application/x-www-form-urlencode
.
我该如何实现这一目标?
附:如果请求没有 Content-Type: application/x-www-form-urlencode
,我得到:
请求包含实体主体但没有Content-Type标头 . 此资源不支持推断的媒体类型'application / octet-stream' . 没有MediaTypeFormatter可用于从媒体类型为'application / octet-stream'.System.Net.Http的内容中读取类型为'CustomerModel'的对象 . System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent内容,类型类型,IEnumerable1格式化程序,System.Net.Http.HttpContentExtensions.ReadAsAsync [T](HttpContent内容,类型类型,IEnumerable1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)中的UnsupportedMediaTypeException System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync上的IFormatterLogger formatterLogger,CancellationToken cancellationToken)(HttpRequestMessage请求,类型类型,IEnumerable`1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)
1 回答
如果请求没有Content-Type标头,那么它将被设置为
application/octet-stream
HTTP / 1.1 RFC 2616规范的Section 7.2.1说明了有关实体主体类型的信息:
尝试先清除所有格式化程序,然后只添加你需要的格式化程序