使用IHttpActionResult时,如何在WebApi2中获取帮助文档?

时间:2013-11-26 09:40:19

标签: rest asp.net-web-api

我想利用WebApi2的自动文档功能以及IHttpActionResult。因此,我想更改以下代码:

/// <summary>
/// Gets specified User 
/// </summary>
/// <param name="id">User Id</param>
/// <returns>The user</returns>
public UserModel Get(int id)
{
    UserModel result = new UserModel()
    {
        ErrorLevel = "Warning",
        ErrorMessage = "Not Implemented yet!"
    };
    User u = new User() { Id = 1, ADUserName = "nfindlater", DefaultRoutingGroupId = 1 };
    result.Data = u;

    var helper = new UrlHelper(Request);
    result.Url = helper.Link("User", new { userId = 1 });

    return result;
}

    /// <summary>
    /// Gets specified User 
    /// </summary>
    /// <param name="id">User Id</param>
    /// <returns>The user</returns>
    public IHttpActionResult Get(int id)
    {
        UserModel result = new UserModel()
        {
            ErrorLevel = "Warning",
            ErrorMessage = "Not Implemented yet!"
        };
        User u = new User() { Id = 1, ADUserName = "nfindlater", DefaultRoutingGroupId = 1 };
        result.Data = u;

        var helper = new UrlHelper(Request);
        result.Url = helper.Link("User", new { userId = 1 });

        return Ok<UserModel>(result);
    }

但是当我这样做时,我在/ Help / Api / GET-2013-12-05-user-id下丢失了部分自动生成的api文档。

以下是丢失的文档部分:

响应正文格式

        application / json,text / json                      样品:

{   “url”:“示例字符串1”,   “数据”:{     “id”:1,     “adUserName”:“示例字符串2”,     “名称”:“样本字符串3”,     “defaultRoutingGroupId”:4   },   “errorLevel”:“示例字符串2”,   “errorMessage”:“示例字符串3” }

1 个答案:

答案 0 :(得分:33)

您可以使用“ResponseType”属性修饰操作,HelpPage会选择此操作以生成示例...

示例:[ResponseType(typeof(UserModel)]