Dekim
Dekim

Reputation: 654

Content-Type - WebAPI - Request Header

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.

Upvotes: 2

Views: 4035

Answers (3)

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.

Upvotes: 0

aditya
aditya

Reputation: 342

generally web api can return both in xml format and json format, therefore while making a call to your web api you need to specify the content type in the header.

Upvotes: 0

Hamid Pourjam
Hamid Pourjam

Reputation: 20764

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"

Upvotes: 3

Related Questions