How to define default values for [FromQuery] param

2019-08-19 01:31发布

I have an action:

[HttpGet]
[Route("foo")]
public ActionResult Foo([FromQuery] MyClass request)
{
    var image = ToImage(WidgetType.MedianSalesPriceSqft, request);
    return File(image.ToByteArray(), "image/png");
} 

below MyClass is defined:

public class MyClass {
    [DefaultValue("90210")]
    public string Zip { get; set; }

    [DefaultValue("5361 Doverton Dr")]
    public string StreetAddress { get; set; }               
}

When I hit /swagger/index.html and want to try out this API, I always have to enter the StreetAddress and Zip values, even though I have default values defined.

Swagger currently provides a schema filter that let's you provide default values for object properties as long as they are not set to be [FromQuery]. Am I missing something simple?

1条回答
放荡不羁爱自由
2楼-- · 2019-08-19 02:23

It looks like the problem has been fixed on the beta version:
https://www.nuget.org/packages/Swashbuckle.AspNetCore/5.0.0-rc2

Lot of changes on the beta version, I had a couple of DocumentFilter and needed to be refactored,
I had to comment some stuff out that could not figure out how to do on the beta.

I added your same action and class, it looks like this:

enter image description here

Once you click on the try it out the values are populated enter image description here

Just if you needed my code is here:
https://github.com/heldersepu/csharp-proj/tree/master/WebApi_NetCore

查看更多
登录 后发表回答