Skip to main content

ELL Blog

ASP.NET Core Global Json Options

When adding JSON options to an ASP.NET controller, these options do not apply to manual uses of the json serializer. Therefore, if you want to use the same options for other serializing or deserializing, you can use dependency injection.

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Api.Services;

public Constructor(IOptions<JsonOptions> jsonOptions) {
    var serializerOptions = jsonOptions.Value.JsonSerializerOptions;

If you don’t know how to set default Json serializer options for the controller, read ASP.NET Core Optional Json Fields.

I’m not sure why this was a draft, but I made it public again.