Created
December 22, 2014 20:56
-
-
Save JamisonWhite/f13456470982586d4d14 to your computer and use it in GitHub Desktop.
Add better browser support and pretty print JSON in C# WebApi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// WebAPI add better browser support and pretty print. | |
/// </summary> | |
/// <remarks> | |
/// Usage: | |
/// WebApiConfig.Register() | |
/// config.Formatters.Add(new BrowserJsonFormatter()); | |
/// ... | |
/// | |
/// Source: | |
/// Todd Menier from StackOverflow | |
/// http://stackoverflow.com/questions/9847564/how-do-i-get-asp-net-web-api-to-return-json-instead-of-xml-using-chrome | |
/// </remarks> | |
public class BrowserJsonFormatter : JsonMediaTypeFormatter | |
{ | |
public BrowserJsonFormatter() | |
{ | |
this.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); | |
this.SerializerSettings.Formatting = Formatting.Indented; | |
} | |
public override void SetDefaultContentHeaders(Type type, HttpContentHeaders headers, MediaTypeHeaderValue mediaType) | |
{ | |
base.SetDefaultContentHeaders(type, headers, mediaType); | |
headers.ContentType = new MediaTypeHeaderValue("application/json"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment