Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

In the creation of our new MVC Web API-based service, we want to focus on XML to begin with and add JSON functionality later as an enhancement, using the full release with native JSON.NET support. To that end, we want to prevent the service accepting requests or giving responses in JSON to avoid establishing any functionality we are expecting to break.

Is there a way to disable JSON support in the ASP.NET MVC API?

share|improve this question

1 Answer

up vote 4 down vote accepted

All you have to do is to remove JSON media formatters.

// Identify JSON formatters in global config.
var jsonMediaTypeFormatters = GlobalConfiguration.Configuration.Formatters
    .Where(x => x.SupportedMediaTypes
    .Any(y => y.MediaType.Equals("application/json", StringComparison.InvariantCultureIgnoreCase)))
    .ToList();

// Remove formatters from global config.
foreach (var formatter in jsonMediaTypeFormatters)
{
    GlobalConfiguration.Configuration.Formatters.Remove(formatter);
}
share|improve this answer
That's the solution I expected. It would be great to see some code on how to do this. – Tragedian Apr 20 '12 at 16:50
@ProgrammingHero there you go. Updated. – Aliostad Apr 20 '12 at 16:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.