Generated WEB API Help Area documentation does not show response body formats/samples
Asked Answered
A

2

8

I have created a help area documentation fro my web api 2 projects (based on owin/katana). I turned on everything setting in the config and installed Microsoft.AspNet.WebApi.OData. Currently I have the following settings:

config.SetSampleObjects(new Dictionary<Type, object>
{
    {typeof(string), "sample string"},
    {typeof(IEnumerable<string>), new string[]{"sample 1", "sample 2"}}
});

config.SetSampleForMediaType(
    new TextSample("Binary JSON content. See http://bsonspec.org for details."),
    new MediaTypeHeaderValue("application/bson"));

config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>));

config.SetSampleRequest("1234", new MediaTypeHeaderValue("text/plain"), "Values", "Put");

config.SetSampleResponse(new ImageSample("../images/aspNetHome.png"), new MediaTypeHeaderValue("image/png"), "Values", "Get", "id");

config.SetActualRequestType(typeof(string), "Values", "Get");

config.SetActualResponseType(typeof(string), "Values", "Post");

However, I don't any response sample being generated. My page supposed to look like this as I saw on the web

what it should look like

but it it loos like this. How do I get to display sample response formats like JSON as shown in the first picture ?

enter image description here

Aguie answered 22/11, 2015 at 18:11 Comment(0)
T
4

Try to decorate your controller action with the ResponseType attribute, like this:

    [HttpGet]
    [Route("enterprise/")]
    [ResponseType(typeof(EnterpriseViewModel))]
    public IHttpActionResult Get()
    {
        ...
    }

If you have a constructor with paramaeters, you also must provide a blank constructor into your ViewModel class.

Tushy answered 13/12, 2016 at 17:9 Comment(1)
Blank constructor! Thanx!Aeneous
N
2

That's because of the content negotiation. You're probably specifying something like this in your response:

config.SetSampleForType("[0]=foo&[1]=bar", new MediaTypeHeaderValue("application/x-www-form-urlencoded"), typeof(IEnumerable<string>))

Which prevents the content negotiation to work correctly and therefore, no JSON format have been generated in the help page.

More here: http://blogs.msdn.com/b/yaohuang1/archive/2012/10/13/asp-net-web-api-help-page-part-2-providing-custom-samples-on-the-help-page.aspx

Nodose answered 27/11, 2015 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.