2

After defining the endpoint

app.MapGet($"{baseRoute}/GetProducts", GetProductsAsync)
.WithName("GetProducts")
.Produces<ProductResponseDto>()
.WithTags(Tag);

With this async method to call implementation.

private static async Task<IResult> GetProductsAsync(ProductsDto getProducts, IUserService userService)
{
    ProductResponseDto getProductsResponse = await userService.GetProductsAsync(getProducts);
    return Results.Ok(getProductsResponse);
}

I defined a basic BindAsync method for bind values. I just want to add all fields contained in ProductsDto into swagger UI. Is it possible? If I use [FromQuery] annotation the Dto is null for every call. I am using .net core 6.

public class ProductsDto
{
    /// <summary>
    /// clientId
    /// </summary>
    public int clientId { get; set; }
    /// <summary>
    /// Test
    /// </summary>
    public int test { get; set; }

    public static ValueTask<ProductsDto> BindAsync(HttpContext context, ParameterInfo parameter)
    {
        int.TryParse(context.Request.Query["clientId"], out var in_clientId);
        int.TryParse(context.Request.Query["test"], out var in_test);
        var result = new ProductsDto
        {
            clientId = in_clientId,
            test = in_test
        };
        return ValueTask.FromResult<ProductsDto?>(result);
    }
}
1
  • Please add all relevant part including ProductsDto and desired url to be hit. Commented Mar 29, 2023 at 14:46

1 Answer 1

1

Is this what you want? Using [AsParameters]. Related document here.

enter image description here

In .net 6, we can use custom model binding...

app.MapGet("/GetProducts2", (ProductsDto getProducts) =>
{
    return Results.Ok();
});

public class ProductsDto
{
    public int clientId { get; set; }
    public int test { get; set; }

    public static bool TryParse(string? value, IFormatProvider? provider, out ProductsDto? prod)
    {
        var segments = value?.Split(',',
                StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
        if (segments?.Length == 2
            && int.TryParse(segments[0], out var x)
            && int.TryParse(segments[1], out var y))
        {
            prod = new ProductsDto { clientId = x, test = y };
            return true;
        }

        prod = null;
        return false;
    }
}

This will make the ProductsDto recognized as a string, and we can have TryParse method to handle the string and bind to the properties.

enter image description here

By the way, we can't use [FromQuery]ProductsDto getProducts. This will make the swagger UI show parameter like this which recognize ProductsDto as a object:

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately I have to use .net core 6 which does not support [AsParameters].
then we need custom binding with TryParse
I updated my post with a workaround in .net 6
Any ideas on how to get the same results when using BindAsync?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.