Change the JSON serialization settings of a single ASP.NET Core controller

后端 未结 2 876
[愿得一人]
[愿得一人] 2020-12-09 08:59

I\'m having two controller controllers: ControllerA and ControllerB. The base class of each controller is Controller.

The

相关标签:
2条回答
  • 2020-12-09 09:29

    Ended up creating this method that I use on my end points:

    {           
        // needed to get the same date and property formatting 
        // as the Search Service:
        var settings = new JsonSerializerSettings
        {
            ContractResolver = new DefaultContractResolver()
            {
                NamingStrategy = new SnakeCaseNamingStrategy()
            },
            DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffZ"
        };
    
        return Json(result, settings);
    }
    
    0 讨论(0)
  • 2020-12-09 09:40

    You can achieve this with a combination of an Action Filter and an Output Formatter. Here's an example of what the Action Filter might look like:

    public class SnakeCaseAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext ctx)
        {
            if (ctx.Result is ObjectResult objectResult)
            {
                objectResult.Formatters.Add(new JsonOutputFormatter(
                    new JsonSerializerSettings
                    {
                        ContractResolver = new DefaultContractResolver
                        {
                            NamingStrategy = new SnakeCaseNamingStrategy()
                        }
                    },
                    ctx.HttpContext.RequestServices.GetRequiredService<ArrayPool<char>>()));
            }
        }
    }
    

    Using OnActionExecuted, the code runs after the corresponding action and first checks to see if the result is an ObjectResult (which also applies to OkObjectResult thanks to inheritance). If it is an ObjectResult, the filter simply adds a customised version of a JsonOutputFormatter that will serialise the properties using SnakeCaseNamingStrategy. The second parameter in the JsonOutputFormatter constructor is retrieved from the DI container.

    In order to use this filter, just apply it to the relevant controller:

    [SnakeCase]
    public class ControllerB : Controller { }
    

    Update for ASP.NET Core 3.0+

    Things look a little different for 3.0+, where the default JSON-formatters for 3.0+ are based on System.Text.Json. At the time of writing, these don't have built-in support for a snake-case naming strategy.

    However, if you're using Json.NET with 3.0+ (details in the docs), the SnakeCaseAttribute from above is still viable, with a couple of changes:

    1. JsonOutputFormatter is now NewtonsoftJsonOutputFormatter.
    2. The NewtonsoftJsonOutputFormatter constructor requires an argument of MvcOptions.

    Here's the code:

    public class SnakeCaseAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuted(ActionExecutedContext ctx)
        {
            if (ctx.Result is ObjectResult objectResult)
            {
                objectResult.Formatters.Add(new NewtonsoftJsonOutputFormatter(
                    new JsonSerializerSettings
                    {
                        ContractResolver = new DefaultContractResolver
                        {
                            NamingStrategy = new SnakeCaseNamingStrategy()
                        }
                    },
                    ctx.HttpContext.RequestServices.GetRequiredService<ArrayPool<char>>(),
                    ctx.HttpContext.RequestServices.GetRequiredService<IOptions<MvcOptions>>().Value));
            }
        }
    }
    

    Note: You might want to create the JsonOutputFormatter/NewtonsoftJsonOutputFormatter ahead of time somewhere, for example - I've not gone that far in the example as that's secondary to the question at hand.

    0 讨论(0)
提交回复
热议问题