13

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?

4
  • What you have tried so far? or have you followed any tutorial to achieve this, if yes, then post link? Oct 19, 2014 at 14:42
  • @ArindamNayak I have not followed any tutorials as I've used Web Api 2 For months where it successfully has generated the sample data which is why I wonder why it won't for this particular example. Oct 19, 2014 at 14:43
  • This might be helpful -stackoverflow.com/questions/14843243/… Oct 19, 2014 at 14:46
  • @ArindamNayak I tried generating the XML file but I'm still left with the same result, strange as it is. Oct 19, 2014 at 14:53

3 Answers 3

15

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);
}
2
  • Thats the easiest fix for sure.
    – J3RM
    Jul 15, 2015 at 17:02
  • Also important to remember that your result class should be public. Jan 10, 2020 at 21:11
9

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

5

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() { }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.