-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
38 lines (32 loc) · 1.21 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Diagnostics;
using System.Text.Json.Serialization;
using TDK_Boilerplate_C_.jsonrpc.response;
using System.Net.Mime;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers().ConfigureApiBehaviorOptions(options => {
options.InvalidModelStateResponseFactory = context =>
{
var errors = context.ModelState.Keys
.SelectMany(key => context.ModelState[key].Errors.Select(x => $"{key}: {x.ErrorMessage}"))
.ToArray();
ResponseDto err = new ResponseDto{
jsonrpc = ProtocolVersion.V2_0,
id = "",
error = new ErrorDto{
code = (int)ErrorCode.INVALID_CONFIGURATION,
message = errors[0],
data = new object()
}
};
var result = new ObjectResult(err);
result.ContentTypes.Add(MediaTypeNames.Application.Json);
return result;
};
});
builder.Services.AddMvc().AddJsonOptions(options => {
options.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull;
});
var app = builder.Build();
app.MapControllers();
app.Run();