Skip to content

Instantly share code, notes, and snippets.

@lekodeveloperweb
Last active August 21, 2023 08:11
Show Gist options
  • Save lekodeveloperweb/e1483e1fd958eb30dddf to your computer and use it in GitHub Desktop.
Save lekodeveloperweb/e1483e1fd958eb30dddf to your computer and use it in GitHub Desktop.
WebApi configuration for json newtonsoft
using System.Web.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace ViVi.Api
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
jsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
config.Formatters.Remove(config.Formatters.XmlFormatter);
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment