How can we make sure that web api returns json data only?
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal YelveRank #178 · 13.6K reputationPosted Oct 10, 2022, 2:42 AM EDT
Open WebApiConfig.cs file in solution and add mentioned line
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")); }Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 10, 2022, 3:36 AM EDT
https://www.c-sharpcorner.com/blogs/return-data-in-json-format-from-web-api