如何在 ASP.NET Core 3.1 中获取当前的 JsonSerializerOptions?
How to get current JsonSerializerOptions in ASP.NET Core 3.1?
我使用 System.Text.Json 并在 Startup.cs
的 ConfigureServices 方法中设置 JsonSerializerOptions
public void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
...
}
现在我想在 CustomErrorHandlingMiddleware 中获取当前的 JsonSerializerOptions
public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception exc)
{
var response = new SomeObject();
var currentJsonSerializerOptions = ? //I want get CurrentJsonSerializerOptions here
var result = System.Text.Json.JsonSerializer.Serialize(response, currentJsonSerializerOptions);
context.Response.ContentType = "application/json";
context.Response.StatusCode = 500;
await context.Response.WriteAsync(result);
}
}
我怎样才能做到这一点?谢谢
您可以根据 Options pattern 将 IOptions<JsonOptions>
或 IOptionsSnapshot<JsonOptions>
注入您的中间件。
public async Task Invoke(HttpContext httpContext, IOptions<JsonOptions> options)
{
JsonSerializerOptions serializerOptions = options.Value.JsonSerializerOptions;
await _next(httpContext);
}
可以写一个继承自ActionResult的中间件class如下
public override Task ExecuteResultAsync(ActionContext context)
{
var httpContext = context.HttpContext;
var response = httpContext.Response;
response.ContentType = "application/json; charset=utf-8";
response.StatusCode = (int) _statusCode;
var options = httpContext.RequestServices.GetRequiredService<IOptions<JsonOptions>>().Value;
return JsonSerializer.SerializeAsync(response.Body, _result, _result.GetType(), options.JsonSerializerOptions);
}
我使用 System.Text.Json 并在 Startup.cs
的 ConfigureServices 方法中设置 JsonSerializerOptions public void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.PropertyNamingPolicy = JsonNamingPolicy.CamelCase;
});
...
}
现在我想在 CustomErrorHandlingMiddleware 中获取当前的 JsonSerializerOptions
public async Task InvokeAsync(HttpContext context)
{
try
{
await _next(context);
}
catch (Exception exc)
{
var response = new SomeObject();
var currentJsonSerializerOptions = ? //I want get CurrentJsonSerializerOptions here
var result = System.Text.Json.JsonSerializer.Serialize(response, currentJsonSerializerOptions);
context.Response.ContentType = "application/json";
context.Response.StatusCode = 500;
await context.Response.WriteAsync(result);
}
}
我怎样才能做到这一点?谢谢
您可以根据 Options pattern 将 IOptions<JsonOptions>
或 IOptionsSnapshot<JsonOptions>
注入您的中间件。
public async Task Invoke(HttpContext httpContext, IOptions<JsonOptions> options)
{
JsonSerializerOptions serializerOptions = options.Value.JsonSerializerOptions;
await _next(httpContext);
}
可以写一个继承自ActionResult的中间件class如下
public override Task ExecuteResultAsync(ActionContext context)
{
var httpContext = context.HttpContext;
var response = httpContext.Response;
response.ContentType = "application/json; charset=utf-8";
response.StatusCode = (int) _statusCode;
var options = httpContext.RequestServices.GetRequiredService<IOptions<JsonOptions>>().Value;
return JsonSerializer.SerializeAsync(response.Body, _result, _result.GetType(), options.JsonSerializerOptions);
}