Try our conversational search powered by Generative AI!

UseNewtonsoftSerialization formats other responses as well


I want to use UseNewtonsoftSerialization for my custom Api Controllers.

// Startup.cs
services.UseNewtonsoftSerialization(typeof(BaseJsonApiController), settings =>
	settings.Converters.Add(new StringEnumConverter(new DefaultNamingStrategy()));
	settings.ContractResolver = new DefaultContractResolver
		NamingStrategy = new CamelCaseNamingStrategy(true, true, true)
	settings.StringEscapeHandling = StringEscapeHandling.EscapeHtml;
	settings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;

This setting some how also affects loading of form data in backend when viewing form submissions.


If I remove the UseNewtonsoftSerialization the form submissions data starts working correctly again.

Any ideas?

Oct 12, 2023 6:48

We had similar issues at our end too when we added some serializer settings to startup, it was interfering with Optimizely forms. To address this we have removed serializer settings from startup which fixes the Optimizely forms.

We then have a custom result object class inheriting JsonResult where we specify the serializer settings and then use that as part of action method. 

You can customize this class per your need. Then use CustomJsonSerializerResult  instead of JsonResult as a return type.

    public class CustomJsonSerializerResult : JsonResult
        public JsonNetResult(object? value) : base(value)
            Settings = new JsonSerializerSettings
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                ContractResolver = new CamelCasePropertyNamesContractResolver()

        public JsonSerializerSettings Settings { get; private set; }

        public override void ExecuteResult(ActionContext context)
            if (context == null)
                throw new ArgumentNullException("context");          

            HttpResponse response = context.HttpContext.Response;
            response.ContentType =
                string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
            if (this.Value == null)

            var scriptSerializer = JsonSerializer.Create(this.Settings);

            using (var sw = new StringWriter())
                scriptSerializer.Serialize(sw, this.Value);

Oct 12, 2023 13:30

I ended up removing the 'UseNewtonsoftSerialization' and just changing our 'react'-frontend to handle enums as ints instead.

That was the only main change that affected us.

I still needed to use the [JsonFormatter] attribute and the 'JsonFormattedResult' to format the json input/output properly.

Oct 13, 2023 6:17

Do note that creating a new JsonSerializerSettings object every time might affect performance, so better create it statically once 👍

Edited, Nov 14, 2023 10:28
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.