问题描述
我正在使用ASP.NET和Swagger,它公开了接受POST的复杂类型.它具有许多具有不同限制长度的字符串字段.如何在Swagger用户界面中反映出来?
I am using ASP.NET and Swagger that exposes a complex type that accepts a POST. It has a number of string fields that have different restricted lengths. How can I reflect that in the Swagger UI?
推荐答案
您可以使用System.ComponentModel.DataAnnotations
中的StringLengthAttribute
注释属性.
You can annotate the properties with the StringLengthAttribute
from System.ComponentModel.DataAnnotations
.
例如:
[StringLength(10)]
public String Name {get;set;}
将变为:
"name": {
"minLength": 0,
"maxLength": 10,
"type": "string"
}
这:
[StringLength(10, MinimumLength = 5)]
public String Name {get;set;}
成为:
"name": {
"minLength": 5,
"maxLength": 10,
"type": "string"
}
除了StringLength
Swashbuckle还支持Range
和RegularExpression
属性.
Besides StringLength
Swashbuckle also supports the Range
and RegularExpression
attributes.
更新
MaxLength
不起作用. StringLength
可以.但是,在Swagger UI中发现此信息有点笨拙.您必须导航到对象的Model
,然后将鼠标悬停在属性上:
MaxLength
does not work. StringLength
does. However, discovering this information in Swagger UI is a bit clumsy. You have to navigate to the Model
of your object and then hover over the property:
这篇关于Swagger中的数据注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!