【发布时间】:2018-02-28 11:01:48
【问题描述】:
我有一个 .NET Web API 2 应用程序,它带有一个返回 HTML 的控制器方法。我想在 swagger 文档中提供示例 HTML,但我无法显示任何内容。这就是我所拥有的:
[HttpGet]
[SwaggerResponse(HttpStatusCode.OK, Type = typeof(string))]
[SwaggerResponse(HttpStatusCode.NotFound)]
[SwaggerResponseContentType("text/html", Exclusive = true)]
[SwaggerResponseExamples(typeof(string), typeof(ExampleProvider))]
public async Task<HttpResponseMessage> Get(Guid id)
{
var example = GetExample();
return new HttpResponseMessage
{
Content = new StringContent(example, Encoding.UTF8, "text/html")
};
}
public class ExampleProvider
{
public object GetExample()
{
return @"
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>hello</h1>
</body>
</html>";
}
}
但是,招摇的示例响应中没有显示任何内容。我在这里缺少任何配置吗?
【问题讨论】:
-
我尝试了您的代码,毕竟我也无法在 swagger 中获得示例,但我注意到一些事情:1. 注释 [SwaggerResponseExample] 需要一个 HttpStatusCode 作为第一个参数。 2. 第二个参数需要是
IExamplesProvider类型,所以ExampleProvider类应该实现IExamplesProvider3. 方法应该是GetExamples()而不是GetExample()。我使用 3.8 版的 Swashbuckle.Examples 对其进行了测试。你有不同的版本吗?至少我能得到对象的例子,但不能得到字符串的例子。
标签: c# asp.net-web-api2 swagger swagger-ui swashbuckle