我有一个.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>";
}
}
但是,示例响应中没有任何内容显示出来。我在这里缺少任何配置吗?
这是替换swashbuckle:Swagger-Net 我一直致力于该项目添加新功能和修复错误。
您的代码将如下所示:
[SwaggerResponse(HttpStatusCode.OK, type: typeof(string), mediaType: "text/html", examples: EXAMPLE)]
public async Task<HttpResponseMessage> Get()
{
return new HttpResponseMessage
{
Content = new StringContent(EXAMPLE, Encoding.UTF8, "text/html")
};
}
const string EXAMPLE = @"
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>hello</h1>
</body>
</html>";
以下是Swagger-UI的外观: http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=HtmlExample#/HtmlExample/HtmlExample_Get