是否可以在dontnetcore api应用程序中向路径变量添加参数验证?

问题描述 投票:0回答:2

给定方法:

/// <param name="containername">The container the file resides in.</param>
/// <param name="fileName">The name of the file to get.</param>
/// <returns>The file specified.</returns>
[HttpGet("{containername}/contentfiles/{fileName}", Name = "Get")]
[ProducesResponseType(typeof(FileResult), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(ErrorResponse), StatusCodes.Status503ServiceUnavailable)]
public ActionResult Get(string containername, string fileName)

我检查容器名和文件名对于我的目的是否有效,然后获取数据。

Swagger自动给我*必填,但没有最小lnegth和最大长度,这很有意义,因为它不知道在哪里找到它们。

我希望我可以做类似的事情:

/// <param name="containername" minimum="3" maximum="63">The container the file resides in.</param>

但是它只是丢弃那些。如何将它们添加到自动生成的swagger文档中(如果需要,我正在使用swashbuckle)?

c# asp.net-core swagger swashbuckle
2个回答
2
投票

我使用ASP.NET Core 3.1 Api项目创建了一个示例项目。


1
投票

我认为您正在寻找的是带注释的类型,在项目中有一些示例:

© www.soinside.com 2019 - 2024. All rights reserved.