Mat*_*LES 5 c# swagger swashbuckle asp.net-core-2.0
当我向 API 添加 swagger 时,我想获得默认值和响应示例。我添加了 NuGet 包并尝试遵循本教程。该SwaggerResponseExample属性工作正常,但SwaggerRequestExample似乎被简单地忽略了。
我的行动定义如下
[SwaggerRequestExample(typeof(int), typeof(PersonExamples.Request))]
[SwaggerResponseExample(200, typeof(PersonExamples.Response))]
/* more attribute & stuff */
public IActionResult Get(int id) { /* blabla */ }
Run Code Online (Sandbox Code Playgroud)
PersonExamples定义如下的类(删除了非相关代码)
public class PersonExamples
{
public class Request : IExamplesProvider
{
public object GetExamples() { return _persons.List().First().Id; }
}
public class Response : IExamplesProvider
{
public object GetExamples() { return _persons.List().First(); }
}
}
Run Code Online (Sandbox Code Playgroud)
这里也是相关的部分 Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddSwaggerGen(conf =>
{
conf.SwaggerDoc(_documentationPrefix, new Info
{
Title = "Global",
Version = "v0",
});
conf.OperationFilter<ExamplesOperationFilter>();
var filePath = Path.Combine(PlatformServices.Default.Application.ApplicationBasePath, "Global.xml");
conf.IncludeXmlComments(filePath);
});
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSwagger(opt =>
{
opt.RouteTemplate = "{documentName}/swagger.json";
});
if (env.IsDevelopment())
{
app.UseSwaggerUI(conf =>
{
conf.SwaggerEndpoint($"/{_documentationPrefix}/swagger.json", "DataService API");
conf.RoutePrefix = "doc/swagger";
});
}
}
Run Code Online (Sandbox Code Playgroud)
当我运行我的项目并转到 swagger.json 页面时,我注意到响应示例编写正确,但找不到请求示例。进一步调试后,我注意到在PersonExamples.Response.GetExamples调用页面时会命中放置在其中的断点,但放置在PersonExamples.Request.GetExamples方法中的断点不会。所以我相信该SwaggerRequestExample属性永远不会调用该方法,甚至可能不会被调用本身。
我是否错误地使用了标签?为什么它永远不会被调用?
| 归档时间: |
|
| 查看次数: |
2158 次 |
| 最近记录: |