Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Api 2 : "Sample not available"

I'm trying to auto generate the help docs for my Web Api.

However I see on one particular method a sample request could not be generated, as shown in the picture:

Image1

Below are the arguments request parameters:

Image2

Why is it unable to generate a sample request format?

like image 786
JensOlsen112 Avatar asked Oct 19 '14 14:10

JensOlsen112


3 Answers

I have found that actions returning an IHttpActionResult had a missing sample. In order to get samples generated for these methods I had to decorate the action with a ResponseTypeAttribute, for example:

[ResponseType(typeof(Model))]
public async Task<IHttpActionResult> Get(int key)
{
    var entity = await dbContext.Models.FindAsync(key);

    if (entity == null)
    {
        return NotFound();
    }

    return Ok(entity);
}
like image 116
Chris Avatar answered Nov 10 '22 05:11

Chris


Make sure any objects you create have an empty constructor. Objects without an empty constructor cannot be 'automatically' created by the Web API Help Page. If creating an empty constructor is not an option, you could 'manually' create them using the config.SetSampleObjects

like image 39
Wade Wright Avatar answered Nov 10 '22 06:11

Wade Wright


For anyone that is curious about which one of these answers between @chris and @wade-wright is correct.

The answer is BOTH. Both are required. So for example if you're doing a VM to consolidate content returned by your API to the necessary data you'd be looking at doing something like:

[ResponseType(typeof(YourVM))]

over top of your controller action

and an empty constructor in your VM:

 public class YourVM
    {
        public YourVM() { }
    }
like image 5
Chris_ Avatar answered Nov 10 '22 07:11

Chris_