SwaggerRequestExample属性在ASP.NET MVC 5(.NET Framework 4.5.2)中不起作用 [英] SwaggerRequestExample attribute does not work in ASP.NET MVC 5 (.NET Framework 4.5.2)

查看:34
本文介绍了SwaggerRequestExample属性在ASP.NET MVC 5(.NET Framework 4.5.2)中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ASP.NET MVC项目中使用Swashbuckle.Examples package(3.10.0)。但是,我不能使请求示例出现在UI中。

配置(SwaggerConfig.cs)

public static void Register()
{
    var thisAssembly = typeof(SwaggerConfig).Assembly;

    GlobalConfiguration.Configuration
        .EnableSwagger(c => {
             c.SingleApiVersion("v1", "TestApp.Web");
             c.IncludeXmlComments(string.Format(@"{0}inTestApp.Web.xml", System.AppDomain.CurrentDomain.BaseDirectory));
             c.OperationFilter<ExamplesOperationFilter>();
             c.OperationFilter<DescriptionOperationFilter>();
             c.OperationFilter<AppendAuthorizeToSummaryOperationFilter>();
                })
            .EnableSwaggerUi(c => { });
 }  

请求示例类

public class EchoRequestExample : IExamplesProvider
{
    public object GetExamples()
    {
        return new EchoInput { Value = 7 } ;
    }
}

public class EchoInput
{
    public int Value { get; set; }
}

操作

[HttpGet]
[Route("Echo")]
[CustomApiAuthorize]
[SwaggerRequestExample(typeof(EchoInput), typeof(EchoRequestExample))]
[ResponseType(typeof(EchoServiceModel))]
public HttpResponseMessage Echo([FromUri] EchoInput model)
{
    var ret = new EchoServiceModel
    {
        Username = RequestContext.Principal.Identity.Name,
        Value = value
    };
    return Request.CreateResponse(HttpStatusCode.OK, ret);
}
Swagger UI显示XML注释和输出元数据(模型和包含默认值的示例),但不显示请求示例。我附加到进程,EchoRequestExample.GetExamples未命中。

问题:如何使SwaggerRequestExample属性在ASP.NET MVC 5中工作?

注意:Windows标识用于授权。

推荐答案

我知道这种感觉,很多开销只是举个例子,我在这方面挣扎了一段时间,所以我创建了自己的虚张声势的叉子,在尝试合并我的想法失败后,我最终分离并重命名了我的项目,并推送到Nuget,它是:Swagger-Net

这样的示例是:

    [SwaggerExample("id", "123456")]
    public IHttpActionResult GetById(int id)
    {

这里是它的完整代码:Swagger_Test/Controllers/IHttpActionResultController.cs#L26

想知道Swagger-UI上是什么样子,这里是:
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=IHttpActionResult#/IHttpActionResult/IHttpActionResult_GetById

这篇关于SwaggerRequestExample属性在ASP.NET MVC 5(.NET Framework 4.5.2)中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆