Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Microsoft Web API Help page - how to create annotations for parameters

Recently i've start playing with new web api help page functionality, that was recently added to web api project template. And i have a notice that some "Additional information" column is always 'none'.

enter image description here

After some looking at markup i found that this info should arrive from attributes

 <td class="parameter-annotations">
                    @if (parameter.Annotations.Count > 0)
                    {
                        foreach (var annotation in parameter.Annotations)
                        {
                            <p>@annotation.Documentation</p>
                        }
                    }
                    else
                    {
                        <p>None.</p>
                    }
                </td>

But what kind of attribute i should use, to populate additional information? Thanks

like image 424
igorGIS Avatar asked Sep 29 '14 10:09

igorGIS


People also ask

What is difference between FromQuery and FromBody?

[FromQuery] - Gets values from the query string. [FromRoute] - Gets values from route data. [FromForm] - Gets values from posted form fields. [FromBody] - Gets values from the request body.

What is parameter binding in Web API?

When Web API calls a method on a controller, it must set values for the parameters, a process called binding. By default, Web API uses the following rules to bind parameters: If the parameter is a "simple" type, Web API tries to get the value from the URI.

How do I create an optional parameter in Web API?

So, how do we make a parameter optional? We can make a route parameter optional by adding “?” to it. The only gimmick is while declaring a route parameter as optional, we must specify a consequent default value for it: [HttpGet("GetById/{id?}")]


1 Answers

See this site for an example of how to add additional information.

It's basically annotating your model, so in your case it would be something like:-

public class Product
{
    /// <summary>
    ///  The id of the product
    /// </summary>
    [Required]
    public int Id { get; set; }

    /// <summary>
    /// The name of the product
    /// </summary>
    [MaxLength(50)]
    public string Name { get; set; }
}

Which would give you an output like this:-

example output

like image 104
Andy Robinson Avatar answered Sep 20 '22 12:09

Andy Robinson