Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I document an optional QueryString parameter in ASP.NET WebApi Help Pages?

The ASP.Net Web Api Help Pages seem to automatically determine if a parameter is in the Request Uri or Body. How can I document option parameters that are QueryString parameters?

For example I have may have a RESTful Url such as

[GET] api/Books?relatedToBookId=xx

Where "relatedToBookId" is an optional queryString parameter.

Normally the parameters that are FromUri or FromBody are put into the comments as

<param name="variableName">blah blah</param>
like image 510
Justin Avatar asked Jul 16 '13 21:07

Justin


People also ask

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?}")]

Is query parameter optional?

As query parameters are not a fixed part of a path, they can be optional and can have default values.

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.


1 Answers

You could do the following and your optional query string parameter info would show up in the HelpPage.

In the below code relatedToBookId is an optional parameter coming from Query String.

    /// <summary>
    /// Gets list of books
    /// </summary>
    /// <param name="relatedToBookId">Your description here</param>
    /// <returns>returns list of books</returns>
    public IEnumerable<Book> GetBooks(int? relatedToBookId = null)

Also, if you would like to mention about this parameter being optional, you can do the following:

  • Go to the installed file (Areas/HelpPage/Views/Help/DisplayTemplates/Parameters.cshtml)

  • Update the condition related to case ApiParameterSource.FromUri to the following:

    case ApiParameterSource.FromUri: <p>Define this parameter in the request <b>URI</b>.</p> if(parameter.ParameterDescriptor.IsOptional) { <p>This parameter is <b>optional</b>.</p> } break;

like image 157
Kiran Avatar answered Oct 21 '22 11:10

Kiran