Ken*_*sta 10 api asp.net-core .net-6.0
我需要 Swagger 生成 XML API 文件文档,包括用于测试操作的 UI。
当我的项目中使用ASP.NET时,生成了deps XML文件,一切正常。
var filePath = Path.Combine(System.AppContext.BaseDirectory, "Minimal_API.xml");
x.IncludeXmlComments(filePath);
Run Code Online (Sandbox Code Playgroud)
当我运行我的项目时,评论不会显示。
/// <summary>
/// Gets the list of all records
/// </summary>
app.MapGet("/weatherforecast2", () =>
{
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateTime.Now.AddDays(index),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
Run Code Online (Sandbox Code Playgroud)
创建新标签:minimal-api
chu*_*lee 10
有同样的问题。
我正在使用 .NET 6.0.202,Swashbuckle.AspNetCore 版本 6.3.1
从这里找到:https ://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/2267
可以通过使用以下结构来完成:
public static class WeatherEndpoints
{
public static void MapWeatherRoutes(this IEndpointRouteBuilder app)
{
app.MapGet("/weatherforecast2", GetWeather);
}
/// <summary>
/// Gets the list of all records
/// </summary>
/// <returns></returns>
private static IResult GetWeather()
{
var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateTime.Now.AddDays(index),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
}
}
Run Code Online (Sandbox Code Playgroud)
然后在Program.cs
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// map endpoints
app.MapWeatherRoutes();
Run Code Online (Sandbox Code Playgroud)
另外,请确保:
csjproj文件包含<GenerateDocumentationFile>如下 <PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile> <---- this needs to be added
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
Run Code Online (Sandbox Code Playgroud)
Program.cs确保选项配置为使用 XML 文件中配置 Swashbuckle 时。也在https://github.com/domaindrivendev/Swashbuckle.AspNetCore#include-descriptions-from-xml-comments中进行了描述builder.Services.AddSwaggerGen(opts =>
{
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
opts.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4834 次 |
| 最近记录: |