【问题标题】:How can I document query string parameters with Swashbuckle?如何使用 Swashbuckle 记录查询字符串参数?
【发布时间】:2017-12-18 14:03:00
【问题描述】:

Swashbuckle 在提取 C# XML cmets 以制作 Swagger API 文档方面做得很好。 Swagger 支持记录查询字符串参数。但是如果有办法在 XML 中记录查询字符串参数以便 Swashbuckle 将它们导出,我还没有找到。

我有类似的东西:

    /// <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();

并且search 不会出现在输出中。我尝试删除 ? 并没有帮助。我怀疑 XML cmets 只是不直接支持这一点。如果这是真的,我正在寻找最佳实践解决方法。我正在考虑在&lt;remarks&gt; 块中添加几个项目符号。有更好的想法吗?

【问题讨论】:

  • 是否有不能在ListUsers 方法中添加search 参数的原因?
  • 如果有一种方法可以将查询字符串参数作为可选参数添加到 ListUsers() 方法中,但我对 C#.Net 比较陌生,不知道(如何)做那个。
  • public IHttpActionResult ListUsers(string search) 应该可以解决问题。它将从查询字符串中设置。
  • 有一个类似的问题(和解决方案):stackoverflow.com/questions/35828328/…

标签: c# xml documentation query-string swashbuckle


【解决方案1】:

@jps 为我指明了正确的方向。谢谢!

我最终得到:

    /// <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;

【讨论】:

    猜你喜欢
    • 2017-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 2013-04-10
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多