Last active
August 21, 2023 08:11
-
-
Save lekodeveloperweb/e1483e1fd958eb30dddf to your computer and use it in GitHub Desktop.
WebApi configuration for json newtonsoft
This file contains 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
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