Код: Выделить всё
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.MapPost("/weatherforecast", (WeatherForecast weatherForecast) => Results.Ok())
.WithName("Add WeatherForecast")
.WithOpenApi(operation =>
{
operation.RequestBody = new OpenApiRequestBody
{
Content =
{
["application/json"] = new OpenApiMediaType
{
Example = new OpenApiObject
{
["date"] = new OpenApiString("2025-07-10"),
["temperatureC"] = new OpenApiInteger(25),
["summary"] = new OpenApiString("Sunny")
}
}
}
};
return operation;
});
app.Run();
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
Код: Выделить всё
"/weatherforecast": {
"post": {
"tags": [
"WebApplication4"
],
"operationId": "Add WeatherForecast",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/WeatherForecast"
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK"
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... t-examples
Мобильная версия