Skip to content

Instantly share code, notes, and snippets.

@robzhu
Last active March 23, 2022 09:51
Show Gist options
  • Save robzhu/804171e2b90cc2a2958f to your computer and use it in GitHub Desktop.
Save robzhu/804171e2b90cc2a2958f to your computer and use it in GitHub Desktop.
OWIN Startup configuration for JSON serialization
using System.Collections.Generic;
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Owin;
using Swashbuckle.Application;
namespace Project
{
public class Startup
{
public void Configuration( IAppBuilder app )
{
var config = new HttpConfiguration();
var defaultSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Converters = new List<JsonConverter>
{
new StringEnumConverter{ CamelCaseText = true },
}
};
JsonConvert.DefaultSettings = () => { return defaultSettings; };
config.Formatters.Clear();
config.Formatters.Add( new JsonMediaTypeFormatter() );
config.Formatters.JsonFormatter.SerializerSettings = defaultSettings;
config.Routes.MapHttpRoute( "api", "{controller}/{id}", defaults: new { controller = "Root", id = RouteParameter.Optional } );
config.MapHttpAttributeRoutes();
config.EnableSwagger( c =>
{
c.IncludeXmlComments( "docs.xml" );
c.SingleApiVersion( "1.0", "Http Request Debugger" );
} ).EnableSwaggerUi();
app.UseWebApi( config );
}
}
}
@OliverRC
Copy link

OliverRC commented Oct 2, 2017

This does not seem to work for middleware though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment