【问题标题】:using ResponseType attribute in swagger在招摇中使用 ResponseType 属性
【发布时间】:2016-09-10 04:47:40
【问题描述】:

我正在为我的 webapi2 文档使用 swagger。我从 github 下载了源代码并尝试根据我的要求更改源代码。但是我遇到了响应类型的问题,即我想在我的一个 get 方法上指定 responsetype 属性,所以我使用了[ResponseType(typeof(List<Student>))]( student 是我的班级,我想在模型模式中返回学生属性),但它在文档中返回 IHttpActionResult 作为模型和 null 值作为模型模式,有没有其他方法可以访问代码中的 responsetype 属性,以便我可以返回我的模型的属性...

请帮帮我...

[ResponseType(typeof(List<Student>))]
        public IHttpActionResult Get()
        { //my code here
}

【问题讨论】:

    标签: asp.net-web-api swagger


    【解决方案1】:

    当我们想要使用在我们的方法中定义的自定义属性时,我们需要像这样访问属性

    var responseType = actionDescriptor.GetCustomAttributes<ourattributename>();
    

    在我的例子中,属性是ResponseTypeAttribute

    所以现在在responseType 中,您将获得该方法的返回类型。 然后可以根据 returnType 和我们在Swagger.Net.XmlCommentDocumentationProvider 中的要求进行相应的代码更改。

    如果您对此有任何问题,请告诉我。

    谢谢。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-16
      • 2018-01-26
      • 1970-01-01
      • 2022-09-28
      • 2017-10-06
      • 2019-01-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多