2017-07-19 62 views
2

您可以在像下面这个例子那样的方法上添加注释,但是如何在请求和响应模型中添加注释?如何在“请求和响应模型”中添加招摇评论?

/// <summary> 
/// my summary 
/// </summary> 
/// <remarks> 
/// remark goes here. 
/// </remarks> 
/// <param name="somepara">Required parameter: Example: </param> 
/// <return>Returns comment</return> 
/// <response code="200">Ok</response> 

回答

3

是一样贝尔巴托夫说,你可以添加注释与SwaggerResponse响应,要求是有点不同的,就像你添加XML注释到你的动作,你应该添加到的参数,下面是一个例子:

using Swagger.Net.Annotations; 
using System; 
using System.Collections.Generic; 
using System.Net; 
using System.Web.Http; 
using System.Web.Http.Results; 

namespace Swagger_Test.Controllers 
{ 
    public class IHttpActionResultController : ApiController 
    { 

     [SwaggerResponse(HttpStatusCode.OK, "List of customers", typeof(IEnumerable<int>))] 
     [SwaggerResponse(HttpStatusCode.NotFound, Type = typeof(NotFoundResult))] 
     public IHttpActionResult Post(MyData data) 
     { 
      throw new NotImplementedException(); 
     } 
    } 

    /// <summary>My super duper data</summary> 
    public class MyData 
    { 
     /// <summary>The unique identifier</summary> 
     public int id { get; set; } 

     /// <summary>Everyone needs a name</summary> 
     public string name { get; set; } 
    } 
} 

而且在招摇,这将是这样的: enter image description here

+0

这里是一个生动的例子: http://swashbuckletest.azurewebsites.net/swagger/ui/index?filter=IHttpActionResult#/IHttpActionResult/IHttpActionResult_Post – HelderSepu

+0

这里是代码背后: https://github.com/ heldersepu/SwashbuckleTest/BLOB/eb89ab65a3e735f022104c153f2f7af21bc8bf48/Swagger_Test /控制器/ IHttpActionResultController.cs – HelderSepu

+0

现在这个有趣的是我在所示的例子添加///

的唯一标识符 ...就像但是说明没有招摇的文档显示,我的模型是在一个单独的项目(这是否重要?) – 001

1

我不知道这是你在谈论什么,但你可以这样

[SwaggerResponse(HttpStatusCode.Unauthorized, "Authorization has been denied for this request")] 

不同的响应添加注释这是你用它来装饰你的控制器的方法属性。

+1

添加响应的意见也可以使用XML来完成 - 响应代码。 – Hezye