C# `正在忽略SwaggerRequestExample`
当我向API中添加swagger时,我希望获得默认值和响应示例。我添加了NuGet包,并尝试跟随。C# `正在忽略SwaggerRequestExample`,c#,swagger,asp.net-core-2.0,swashbuckle,C#,Swagger,Asp.net Core 2.0,Swashbuckle,当我向API中添加swagger时,我希望获得默认值和响应示例。我添加了NuGet包,并尝试跟随。SwaggerResponseExample属性工作正常,但似乎忽略了SwaggerRequestExample 我的行为定义如下 [SwaggerRequestExample(typeof(int), typeof(PersonExamples.Request))] [SwaggerResponseExample(200, typeof(PersonExamples.Response))] /*
SwaggerResponseExample
属性工作正常,但似乎忽略了SwaggerRequestExample
我的行为定义如下
[SwaggerRequestExample(typeof(int), typeof(PersonExamples.Request))]
[SwaggerResponseExample(200, typeof(PersonExamples.Response))]
/* more attribute & stuff */
public IActionResult Get(int id) { /* blabla */ }
PersonExamples
类定义如下(非相关代码已删除)
这也是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";
});
}
}
public void配置服务(IServiceCollection服务)
{
services.AddSwaggerGen(conf=>
{
conf.SwaggerDoc(_documentationPrefix,新信息
{
Title=“全球”,
Version=“v0”,
});
conf.OperationFilter();
var filePath=Path.Combine(PlatformServices.Default.Application.ApplicationBasePath,“Global.xml”);
conf.includexmlcoments(文件路径);
});
}
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
app.UseSwagger(opt=>
{
opt.RouteTemplate=“{documentName}/swagger.json”;
});
if(env.IsDevelopment())
{
app.UseSwaggerUI(conf=>
{
conf.SwaggerEndpoint($“/{U documentationPrefix}/swagger.json”,“数据服务API”);
conf.RoutePrefix=“doc/swagger”;
});
}
}
当我运行我的项目并转到swagger.json页面时,我注意到响应示例编写得很好,但是请求示例却找不到。在进一步调试之后,我注意到当调用页面时,PersonExamples.Response.GetExamples
中的断点将被命中,但是PersonExamples.Request.GetExamples
方法中的断点不会被命中。因此我相信,SwaggerRequestExample
属性从不调用该方法,甚至可能不会自己调用
我是否不当地使用了标签?为什么从未调用过它?我知道这个问题已经很老了,但Swagger示例不支持GET请求参数(查询参数)。它仅在参数位于请求正文(例如:POST请求)中时起作用。您是在使用NuGet包,还是尝试手动实现代码?如果您没有使用NuGet包,我建议您先尝试一下,因为示例代码中可能有一个微妙的错误,而在发布的实际代码中不一定存在。我使用的是NuGet包。
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";
});
}
}