Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the help documentation to work in WebApi2 when using IHttpActionResult?

I want to take advantage of the automated documentation features of WebApi2 as well IHttpActionResult. Therefore I would like to change the following piece of code:

/// <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;
}

To

    /// <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);
    }

But when I do this I loose part of the automatically generated api documentation under /Help/Api/GET-2013-12-05-user-id.

Here is the part of the documentation that is lost:

Response body formats

application/json, text/json Sample:

{ "url": "sample string 1", "data": { "id": 1, "adUserName": "sample string 2", "name": "sample string 3", "defaultRoutingGroupId": 4 }, "errorLevel": "sample string 2", "errorMessage": "sample string 3" }
like image 641
Nigel Findlater Avatar asked Nov 26 '13 09:11

Nigel Findlater


People also ask

How to return response in Web API?

Depending on which of these is returned, Web API uses a different mechanism to create the HTTP response. Convert directly to an HTTP response message. Call ExecuteAsync to create an HttpResponseMessage, then convert to an HTTP response message. Write the serialized return value into the response body; return 200 (OK).

How do I return IHttpActionResult in Web API?

IHttpActionResult contains a single method, ExecuteAsync, which asynchronously creates an HttpResponseMessage instance. If a controller action returns an IHttpActionResult, Web API calls the ExecuteAsync method to create an HttpResponseMessage. Then it converts the HttpResponseMessage into an HTTP response message.

Can you return an action from Web API?

An action method in Web API 2 can return an implementation of IHttpActionResult class which is more or less similar to ActionResult class in ASP.NET MVC.


1 Answers

You can decorate the action with the "ResponseType" attribute and HelpPage would pick this up to generate the sample...

Example : [ResponseType(typeof(UserModel)]

like image 169
Kiran Avatar answered Oct 22 '22 18:10

Kiran