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:
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" }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).
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.
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.
You can decorate the action with the "ResponseType" attribute and HelpPage would pick this up to generate the sample...
Example : [ResponseType(typeof(UserModel)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With