Blog Post

WebApi Helppage does not display IHttpActionResult method

Tuesday, February 23, 2016 10:31 AM

Today wanted to create WebApi and use help page to help my users to understand my api.

For my example I am creating Account controller to handle my account api calls.

 

My starting point for controller is following code: 

  
public class AccountController : ApiController
    {
        [HttpPost]
        [ResponseType(typeof(Boolean))]
        public IHttpActionResult Login()
        {
            return Ok(true);
        }
    }

This has left me with no documentation. Therefore I have updated the controller to method what was like in example api controller. And introduced following method

 public IEnumerable<string> Get()
{
return new List<string>() { "test", "test2"};
}

This did not help too, but according to default example it should.

No documentation generated

After "bingle" the help page issue I have found the solution (StackOverflow answers). 

I have to enable documentation file to be generated on build, and this is what provides data for the help page.

 

1, In project go to properties

2, Build

3, Insert the location for App_Data (In my case App_Data\Documentation\docummentation.XML)

After rebuild you will have file located there full of nice xml.

 

So what about the IHttpActionResult

You might notice that the document with plain get has been generated but the login method has not.

First I want to setup return type that is returned, so the documentation does know what I have been using.

I do that using 

[ResponseType(typeof(ChangePasswordModel))]

Also I added route, so the api route is exposed as I have wrote method that is not standard rest route.

[Route("Login")]

 

Now I rebuild the solution and I can see routes, yet they are not what I would want to end up with.

 

This routing is a bit messy. 

I want to all routes from api/account

Lets add route prefix on the api controller to start with

[RoutePrefix("api/account")]

 

Final code for documentation

Here is my full final controller code that produces documentation.

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Http;
using System.Web.Http.Description;

namespace wsTmAuth.Controllers
{  
[RoutePrefix("api/account")]     public class AccountController : ApiController     {         /// <summary>
/// Allows user to login
/// </summary>
/// <param name="model">Object containing login requests</param>
/// <returns>success</returns>         [Route("Login")]         [ResponseType(typeof(UserModel))]         public IHttpActionResult Login(LoginModel model)         {             if (!ModelState.IsValid)             {                 return this.BadRequest(ModelState);             }             return Ok(new UserModel());         }
        ///         /// Allows user to change password         ///         ///Password change model         ///         [Route("ChangePassword")]         [HttpPost]         [ResponseType(typeof(ChangePasswordModel))]         public IHttpActionResult ChangePassword(ChangePasswordModel model)         {             if (!ModelState.IsValid)             {                 return this.BadRequest(ModelState);             }             return Ok(true);         }           }     public class ChangePasswordModel     {         public string UserName { get; set; }         public string OldPassword { get; set; }         public string NewPassword { get; set; }     }     public class LoginModel     {         [Required(ErrorMessage = "Username is required", AllowEmptyStrings = false)]         public string Username { get; set; }         [Required(ErrorMessage = "Password is required", AllowEmptyStrings = false)]         public string Password { get; set; }         public int InterfaceContextId { get; set; }     }      public class UserModel
{         public int UserId { get; set; }         public string First { get; set; }         public string Last { get; set; }         public string Email { get; set; }
} }