2

Swashbuckle does a great job of extracting C# XML comments to make Swagger API documentation. And Swagger supports documenting query string parameters. But if there's a way to document query string parameters in XML so Swashbuckle will export them, I haven't found it.

I have something like:

    /// <summary>
    /// List all users
    /// </summary>
    /// <param name="?search">String to search for in user IDs and names</param>
    /// <returns>An array of users</returns>
    /// <response code="200">OK</response>
    [ResponseType(typeof(IEnumerable<User>))]
    [Route("")]
    [HttpGet]
    public IHttpActionResult ListUsers()
    {
        IEnumerable<User> users;

        // "?search=<substring>" in the URI searches for users
        // Adapted from https://stackoverflow.com/questions/10656841
        var searchString = Request.GetQueryNameValuePairs()
            .Where(nv => nv.Key == "search")
            .Select(nv => nv.Value)
            .DefaultIfEmpty("")
            .FirstOrDefault();

And search does not show up in the output. I tried removing the ? and that didn't help. I suspect that the XML comments just don't support this directly. If that's true, I'm looking for a Best Practice work around. I'm considering adding a couple of bullets in a <remarks> block. Any better ideas?

4
  • Is there a reason you can't add a search parameter to the ListUsers method? Commented Dec 18, 2017 at 14:31
  • If there is a way to add query string parameters as optional parameters to the ListUsers() method that could work but I'm relatively new to C#.Net and don't know (how) to do that. Commented Dec 18, 2017 at 14:39
  • public IHttpActionResult ListUsers(string search) should do the trick. It will be set from the query string. Commented Dec 18, 2017 at 14:42
  • There's a similar problem (and solution): stackoverflow.com/questions/35828328/… Commented Dec 18, 2017 at 15:06

1 Answer 1

2

@jps pointed me in the right direction. Thanks!

I ended up with:

    /// <summary>
    /// List all users
    /// </summary>
    /// <param name="search">String to search for in user IDs and names</param>
    /// <returns>An array of users</returns>
    /// <response code="200">OK</response>
    [ResponseType(typeof(IEnumerable<User>))]
    [Route("")]
    [HttpGet]
    public IHttpActionResult ListUsers([Optional]string search)
    {
        IEnumerable<User> users;

        var searchString = search == null ? string.Empty : search;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.