简体   繁体   English

带有 Swagger 文档的可选 WebAPI 路由参数

[英]Optional WebAPI routing parameters with Swagger documentation

I have a WebAPI method with routing defined in an attribute, having one mandatory parameter and one optional:我有一个在属性中定义路由的 WebAPI 方法,有一个强制参数和一个可选参数:

    [HttpGet]
    [Route("api/ChargeCard/{cif}/{feeScheme=null}")]
    [ResponseType(typeof(ChargeCardRoot))]
    public IHttpActionResult Get(string cif, string feeScheme, ChargeCardRequestMode mode = ChargeCardRequestMode.Basic)
    {

I also use Swashbuckle / Swagger to generate documentation.我还使用 Swashbuckle / Swagger 来生成文档。 The problem is that Swagger always marks my optional parameter as required.问题是 Swagger 总是将我的可选参数标记为必需。

Changing optional parameter notation to:将可选参数表示法更改为:

    [Route("api/ChargeCard/{cif}/{feeScheme?}")]

makes both parameters acting like they are required, it doesn't make Swagger to show it as optional either.使这两个参数都像必需的一样,它也不会使 Swagger 将其显示为可选。

Is there a way to generate correct documentation for optional parameters with Swagger?有没有办法使用 Swagger 为可选参数生成正确的文档?

If you overload your methods, Swashbuckle will generate two different Swagger endpoints.如果您重载您的方法,Swashbuckle 将生成两个不同的 Swagger 端点。 One method has the parameter, the other does not and calls the first one with the default value for the "missing" parameter.一种方法有参数,另一种没有,并使用“缺失”参数的默认值调用第一个方法。 This also has the advantage of making it easier if you using something like HyprLinkr to generate HATEOAS links, as you can't have optional parameters in an expression.如果您使用 HyprLinkr 之类的东西来生成 HATEOAS 链接,这也有一个好处,因为您不能在表达式中使用可选参数。

[HttpGet]
[Route("api/ChargeCard/{cif}/{feeScheme}")]
[ResponseType(typeof(ChargeCardRoot))]
public IHttpActionResult Get(string cif, string feeScheme, ChargeCardRequestMode mode = ChargeCardRequestMode.Basic)
{
    // working code
}

[HttpGet]
[Route("api/ChargeCard/{cif}")]
[ResponseType(typeof(ChargeCardRoot))]
public IHttpActionResult Get(string cif, string feeScheme)
{
    return Get(cif, feeScheme, ChargeRequestMode.Basic);
}

Hope that helps.希望有帮助。

As far as Swagger is concerned, you could specify optional parameters by omitting them from the route and specifying "string?"就 Swagger 而言,您可以通过从路由中省略它们并指定“字符串?”来指定可选参数。 for the parameter declarations in the method signature as follows:对于方法签名中的参数声明如下:

[HttpGet]
[Route("api/ChargeCard/{cif}")]
[ResponseType(typeof(ChargeCardRoot))]
public IHttpActionResult Get(string cif, string? feeScheme)
{
    ...
}

Note however, that these would no longer get passed as part of the route.但是请注意,这些将不再作为路线的一部分传递。 The optional parameters would get passed as query parameters appended to the route, as follows:可选参数将作为附加到路由的查询参数传递,如下所示:

https://somedomain.com/api/ChargeCard/{cif}?feeScheme=ABCD

If you take this approach, you would also need to add this line to the top of your file to prevent Visual Studio from removing the "?"如果您采用这种方法,您还需要将此行添加到文件的顶部,以防止 Visual Studio 删除“?” and replacing it with [CanBeNull] attribute:并将其替换为 [CanBeNull] 属性:

#nullable enable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM