Is it possible for WEBAPI controller to return JSON, even if the header "Content-Type" is not present?
In the default setup, when the header is not present, Exception "No MediaTypeFormatter is available to read an object of type ..." is raising.
add this to your WebApiConfig.cs
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html") );
now you get json result unless when you send text/xml
as "Content-Type"
通常,Web API可以XML格式和JSON格式返回,因此在调用Web API时,您需要在标头中指定内容类型。
It is possible to do so. Just change the default serializer to Json Serializer
. More details explained here .
Note: The default serializer of WebApi
is Json Serializer
. So, even if no content type headers are found on the request, you should get the response in json
format.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.