[英]SwashBuckle Swagger-UI Example Request for HTTP GET Method with FromQuery Attribute
我已經設法使用SwashBuckle.AspNetCore
和Swashbuckle.AspNetCore.Filters
為我的 Web API 添加示例以用於 POST 方法:
DTO
public class ExampleDTO
{
public string MyFoo { get; set; }
}
示例請求
public class ExampleDTOExample : IExamplesProvider<ExampleDTO>
{
public ExampleDTO GetExamples()
{
return new ExampleDTO()
{
MyFoo = "bar"
};
}
}
控制器方法
[SwaggerOperation(
Summary = "...",
Description = "...",
OperationId = "PostFoo"
)]
[SwaggerResponse(200, "Returns ...", typeof(int))]
[HttpPost]
[Route("post-foo")]
public ActionResult<int> PostFoo([FromBody]ExampleDTO request)
{
throw new NotImplementedException();
}
這項工作非常好。 當我單擊“試用”按鈕時,我將“bar”作為屬性 foo 的預填充值。
但是,當我嘗試對 GET 請求執行相同操作時,例如,使用來自這樣的查詢的參數時,文本框未預填充值“bar”:
public class ExampleDTO
{
[FromQuery(Name = "foo")]
public string MyFoo { get; set; }
}
控制器方法
[SwaggerOperation(
Summary = "...",
Description = "...",
OperationId = "GetFoo"
)]
[SwaggerResponse(200, "Returns ...", typeof(int))]
[HttpGet]
[Route("get-foo")]
public ActionResult<int> GetFoo([FromQuery]ExampleDTO request)
{
throw new NotImplementedException();
}
如何強制使用示例值預填充文本框? 到目前為止,我已經找到了一個解決方案來指定一個我不想要的默認值。 我只想在 Swagger UI 中使用屬性作為默認值。
那不是示例而是默認值。
這是我過去做過的事情:
"/attrib/{payId}": {
"get": {
"tags": [
"Attribute"
],
"operationId": "Attribute_Get",
"consumes": [],
"produces": [
"application/json",
"text/json",
"text/html"
],
"parameters": [
{
"name": "payId",
"in": "path",
"required": true,
"type": "integer",
"format": "int32",
"default": 123
}
]
http://swagger-net-test.azurewebsites.net/swagger/ui/index?filter=Attribute#/Attribute/Attribute_Get
這是默認和示例的另一種情況
"Company": {
"required": [
"Id",
"MyId"
],
"properties": {
"Id": {
"description": "The Unique Company ID",
"example": "123",
"type": "integer",
"format": "int32",
"default": 456
},
"MyId": {
"example": 123,
"type": "integer",
"format": "int32"
},
http://swagger-net-test.azurewebsites.net/swagger/ui/index#/Company/Company_Get2
您可以看到該示例不是 Swagger UI 中顯示的內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.