You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
how i can configure .net 6 (Minimal API) to use the JsonApiSerializer as a default Json format??
i can set it in each map but i want to set it as an default format
this is will working
app.MapGet("/GetAll", async (AppDBContext db) =>
JsonConvert.SerializeObject(await db.Banks.ToArrayAsync(), new JsonApiSerializerSettings()));
but the output is a text not json
The text was updated successfully, but these errors were encountered:
I'm afraid that overriding the json serializer is not possible in minimal apis. (correct me if I'm wrong) This was a design decision by the ASP.NET Core team, to keep minimal apis simple and performant.
So you'd have to use JsonConvert.SerializeObject within all handlers like you already do.
Additionally, SerializeObject returns a string. That's why ASP.NET returns a Content-Type of text/plain.
To make this approach work correctly with minimal apis,
you need to return a Results.Content response and set your own Content-Type.
For example:
app.MapGet("/hello-world",async()=>{// get a list of banks from the databasevarbanks=awaitdb.Banks.ToListAsync();// serialize to a jsonapi formatted stringvarjsonResponse=JsonConvert.SerializeObject(banks,newJsonApiSerializerSettings());// return a ContentResult with an optional http status codereturnResults.Content(jsonResponse,"application/vnd.api+json",200);})
how i can configure .net 6 (Minimal API) to use the JsonApiSerializer as a default Json format??
i can set it in each map but i want to set it as an default format
this is will working
app.MapGet("/GetAll", async (AppDBContext db) =>
JsonConvert.SerializeObject(await db.Banks.ToArrayAsync(), new JsonApiSerializerSettings()));
but the output is a text not json
The text was updated successfully, but these errors were encountered: