rest - 使用 IHttpActionResult 时,如何获取帮助文档以在 WebApi2 中工作?

标签 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 文档的一部分。

这是丢失的文档部分:

响应体格式

应用程序/json,文本/json

样本:

{
"url": "示例字符串 1",
“数据”: {
“身份证”:1,
"adUserName": "示例字符串 2",
"name": "示例字符串 3",
“defaultRoutingGroupId”:4
},
"errorLevel": "示例字符串 2",
"errorMessage": "示例字符串 3"
}

最佳答案

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

示例:[ResponseType(typeof(UserModel)]

关于rest - 使用 IHttpActionResult 时,如何获取帮助文档以在 WebApi2 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20213309/

相关文章:

rest - 用于创建和更新资源的超媒体友好 REST 模式

c# - 在 web api 中编辑方法

升级到 Visual Studio 2012 RC : Method not found: 'Void System.Net.Http.Headers.HttpHeaders.AddWithoutValidation 后出现 ASP.NET Web API 错误

c# - 处理抛出 Controller 方法参数的构造函数?

带有 MVC、ContentNegotation、REST 和 PDF 生成器的 Spring 3.2

design-patterns - 创建 REST 服务 API 的初学者指南(包括 RESTful 身份验证)

xml - 从 WebRequest 模拟 WebResponse

java - JAX-RS 的默认字符编码是什么?

c# - 无法将类型 'System.Web.Http.Results.NotFoundResult' 隐式转换为 'System.Collections.Generic.IEnumerable<....>'

entity-framework - net core odata put和patch嵌套数据结构