使用Swagger和Swashbuckle的Html示例响应

问题描述 投票:0回答:1

我有一个.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>";
    }
}

但是,示例响应中没有任何内容显示出来。我在这里缺少任何配置吗?

c# asp.net-web-api2 swagger swagger-ui swashbuckle
1个回答
0
投票

这是替换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

© www.soinside.com 2019 - 2024. All rights reserved.