在响应中返回对象时遇到问题Swashbuckle中的示例。示例

时间:2019-05-30 21:56:44

标签: c# asp.net-web-api2 swagger-ui swashbuckle.examples

我在Web API中使用Swashbuckle.Examples以获得更好的文档。对于Swashbuckle样本响应,它工作正常,但是当我使用“样本示例”时 当我运行项目时,它显示错误。

我的控制器

 [SwaggerResponse(HttpStatusCode.OK, Type = typeof(IEnumerable<ReasonReponsesuccessMessage_list>))]
 [SwaggerResponseExample(HttpStatusCode.OK, typeof(IEnumerable<ReasonReponseSuccessExample_list>))]
 [SwaggerResponse(HttpStatusCode.BadRequest, Type = typeof(IEnumerable<ReponseEmptyMessage>))]
 [SwaggerOperation("List reasons")]
 [ActionName("Reasons")]
 [Route("api/{Id}")]
 [HttpGet]
 public HttpResponseMessage GetReasons(string Id)
 {

 }

响应示例类

  public class ReasonReponseSuccessExample_list : IExamplesProvider
    {
        object IExamplesProvider.GetExamples()
        {
            ReasonReponsesuccessMessage_list ReasonReponsesuccessMessage_list = new ReasonReponsesuccessMessage_list();

            ReasonReponsesuccessMessage_list.Message = "Success";
            ReasonReponsesuccessMessage_list.Data = new List<tbl_reason>
            {
                new tbl_reason{ id="SAA133",primary_name="Wrong Invoice",alt_name="Wrong Invoice"},
                new tbl_reason{ id="B97123",primary_name="Payment Problem",alt_name=""}
            };
            ReasonReponsesuccessMessage_list.Extras = "";
            ReasonReponsesuccessMessage_list.Success = true;
            return ReasonReponsesuccessMessage_list;
        }
    }

错误

  

要实现的预期examplesProviderType   Swashbuckle.Examples.IExamplesProvider。   System.Collections.Generic.IEnumerable`1 [IgniteAPI.Payload.ReasonReponseSuccessExample_list]   没有。

我在global.asmx中遇到此错误

GlobalConfiguration.Configure(WebApiConfig.Register);

1 个答案:

答案 0 :(得分:0)

您会在错误中看到,您需要指定实现的类型  IExamplesProvider

使用

[SwaggerResponseExample(HttpStatusCode.OK, typeof(ReasonReponseSuccessExample_list))]

代替

[SwaggerResponseExample(HttpStatusCode.OK, typeof(IEnumerable<ReasonReponseSuccessExample_list>))]