ASP.NET Web API does content negotiation by default - will return XML or JSON or other type based on the Accept header. I don't need / want this, is there a way (like an attribute or something) to tell Web API to always return JSON?
- 50,745
 - 59
 - 165
 - 240
 
- 
                    You may be able to do this removing all formatters except json from `GlobalConfiguration.Configuration.Formatters` – Claudio Redi Sep 27 '12 at 20:05
 
10 Answers
Clear all formatters and add Json formatter back.
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
EDIT
I added it to Global.asax inside Application_Start().
- 6,442
 - 5
 - 37
 - 43
 
- 27,097
 - 6
 - 95
 - 82
 
- 
                    
 - 
                    
 - 
                    6Filip W just got better way now :), see it here http://www.strathweb.com/2013/06/supporting-only-json-in-asp-net-web-api-the-right-way/ – Tien Do Jun 15 '13 at 16:36
 - 
                    8
 - 
                    The settings could be better put in `App_Start\WebApiConfig.cs` file. – Will Huang Jan 07 '22 at 02:39
 
Supporting only JSON in ASP.NET Web API – THE RIGHT WAY
Replace IContentNegotiator with JsonContentNegotiator:
var jsonFormatter = new JsonMediaTypeFormatter();
//optional: set serializer settings here
config.Services.Replace(typeof(IContentNegotiator), new JsonContentNegotiator(jsonFormatter));
JsonContentNegotiator implementation:
public class JsonContentNegotiator : IContentNegotiator
{
    private readonly JsonMediaTypeFormatter _jsonFormatter;
    public JsonContentNegotiator(JsonMediaTypeFormatter formatter) 
    {
        _jsonFormatter = formatter;    
    }
    public ContentNegotiationResult Negotiate(
            Type type, 
            HttpRequestMessage request, 
            IEnumerable<MediaTypeFormatter> formatters)
    {
        return new ContentNegotiationResult(
            _jsonFormatter, 
            new MediaTypeHeaderValue("application/json"));
    }
}
- 30,789
 - 8
 - 97
 - 121
 
- 
                    4where does the first part of the code get cut and pasted too? I don't see a "config" object in my Global.asax. Where is that variable coming from? the article doesn't explain either. – BuddyJoe Aug 13 '13 at 23:38
 - 
                    3Check public static void Register(HttpConfiguration config) { ... } method in WebApiConfig.cs file which has been gererated by VS2012 on project creation – Dmitry Pavlov Aug 14 '13 at 08:56
 - 
                    Will this force JSON in the sense that a client `Accept`ing XML will get JSON, and **will not get a 406**? – Luke Puplett Mar 26 '14 at 12:32
 - 
                    1I can answer my own comment/question: it returns XML whatever the `Accept` header. – Luke Puplett Mar 26 '14 at 18:19
 - 
                    2This breaks my swashbuckle integration and it seems it is related to this issue on github ( https://github.com/domaindrivendev/Swashbuckle/issues/219 ). I'd like to use this method but the one below using `GlobalConfiguration...Clear()` actually works. – seangwright May 07 '15 at 20:52
 
Philip W had the right answer but for clarity and a complete working solution, edit your Global.asax.cs file to look like this: (Notice I had to add the reference System.Net.Http.Formatting to the stock generated file)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace BoomInteractive.TrainerCentral.Server {
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class WebApiApplication : System.Web.HttpApplication {
        protected void Application_Start() {
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            //Force JSON responses on all requests
            GlobalConfiguration.Configuration.Formatters.Clear();
            GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
        }
    }
}
- 4,706
 - 1
 - 37
 - 25
 
GlobalConfiguration.Configuration.Formatters.XmlFormatter.SupportedMediaTypes.Clear();
This clears the XML formatter and thus defaulting to JSON format.
- 6,717
 - 10
 - 56
 - 67
 
Inspired by Dmitry Pavlov's excellent answer, I altered it slightly so I could plug-in whatever formatter I wanted to enforce.
Credit to Dmitry.
/// <summary>
/// A ContentNegotiator implementation that does not negotiate. Inspired by the film Taken.
/// </summary>
internal sealed class LiamNeesonContentNegotiator : IContentNegotiator
{
    private readonly MediaTypeFormatter _formatter;
    private readonly string _mimeTypeId;
    public LiamNeesonContentNegotiator(MediaTypeFormatter formatter, string mimeTypeId)
    {
        if (formatter == null)
            throw new ArgumentNullException("formatter");
        if (String.IsNullOrWhiteSpace(mimeTypeId))
            throw new ArgumentException("Mime type identifier string is null or whitespace.");
        _formatter = formatter;
        _mimeTypeId = mimeTypeId.Trim();
    }
    public ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
    {
        return new ContentNegotiationResult(_formatter, new MediaTypeHeaderValue(_mimeTypeId));
    }
}
- 42,091
 - 47
 - 181
 - 266
 
This has correct headers set. Seems a bit more elegant.
public JsonResult<string> TestMethod() 
{
return Json("your string or object");
}
- 604
 - 4
 - 15
 
- 
                    1What are the fully qualified names for the JsonResult and Json classes? – Josh Withee Dec 19 '18 at 17:42
 - 
                    https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.jsonresult?view=aspnet-mvc-5.2 – Netferret Jan 11 '19 at 17:02
 
If you want to do that for one method only, then declare your method as returning HttpResponseMessage instead of IEnumerable<Whatever> and do:
    public HttpResponseMessage GetAllWhatever()
    {
        return Request.CreateResponse(HttpStatusCode.OK, new List<Whatever>(), Configuration.Formatters.JsonFormatter);
    }
this code is pain for unit testing but that's also possible like this:
    sut = new WhateverController() { Configuration = new HttpConfiguration() };
    sut.Configuration.Formatters.Add(new Mock<JsonMediaTypeFormatter>().Object);
    sut.Request = new HttpRequestMessage();
- 1,399
 - 1
 - 10
 - 22
 
- 
                    If you want something for a method only create an http://msdn.microsoft.com/en-us/library/system.web.mvc.actionfilterattribute(v=vs.118).aspx – Elisabeth Jan 06 '14 at 15:14
 
for those using OWIN
GlobalConfiguration.Configuration.Formatters.Clear();
GlobalConfiguration.Configuration.Formatters.Add(new JsonMediaTypeFormatter());
becomes (in Startup.cs):
   public void Configuration(IAppBuilder app)
        {
            OwinConfiguration = new HttpConfiguration();
            ConfigureOAuth(app);
            OwinConfiguration.Formatters.Clear();
            OwinConfiguration.Formatters.Add(new DynamicJsonMediaTypeFormatter());
            [...]
        }
- 86
 - 5
 
Yo can use in WebApiConfig.cs:
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
- 1
 
 public System.Web.Http.Results.JsonResult<MeineObjekt> Get()
    {
        return Json(new MeineObjekt()
        {
            Cod = "C4666",               
            Payment = 10.0m,
            isEnough = false
        });
    }
- 973
 - 11
 - 25