By default, Web API produces XML but if there is need for JSON, given syntax will do it.
Open WebApiConfig.cs file in solution and add mentioned line in it as shown in example.
- public static void Register(HttpConfiguration config)
- {
- config.Routes.MapHttpRoute(name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new
- {
- id = RouteParameter.Optional
- });
- //To produce JSON format add this line of code
- config.Formatters.JsonFormatter.SupportedMediaTypes.Add(newMediaTypeHeaderValue("text/html"));
- }

Ravindar KumarPosted Jun 6, 2019, 1:17 AM
Thanks for the post its save my day. I have implemented this in my new project and thanks again.
Peterson AtoePosted Aug 22, 2018, 4:11 PM
Config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
Hrishikesh KulkarniPosted Feb 7, 2018, 8:20 PM
Thank You......!This is Good one Example.
sathis kumarPosted Nov 26, 2017, 7:45 PM
Server Error in '/' Application.The method or operation is not implemented. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NotImplementedException: The method or operation is not implemented. Source Error: Line 28: private static MediaTypeHeaderValue newMediaTypeHeaderValue(string v) Line 29: { Line 30: throw new NotImplementedException(); Line 31: } Line 32: }
Ajay SharmaPosted Oct 8, 2017, 3:28 AM
Minor correction and information i.e. 1) using System.Net.Http.Headers namespace should be used. 2) Also there is space should be space between new and MediaType (new MediaTypeHeaderValue).
Sujeet KumarPosted Jun 15, 2016, 5:52 AM
This is Good One !!
Samir SanghaviPosted Feb 16, 2016, 7:24 AM
Thank You..!!
marcioPosted Nov 4, 2015, 8:53 PM
Very good!!!! tks!
Sumit RKPosted Oct 13, 2015, 6:56 AM
Good
Guest UserPosted Jul 31, 2014, 8:39 AM
welcome Mr. Bajaj to the website!