Skip to content

Consider adding an API that enables invoking the default parameter binding logic of RequestDelegateFactory explicitly #38003

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
DamianEdwards opened this issue Nov 1, 2021 · 0 comments
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-minimal-actions Controller-like actions for endpoint routing Priority:3 Work that is nice to have triage-focus Add this label to flag the issue for focus at triage
Milestone

Comments

@DamianEdwards
Copy link
Member

DamianEdwards commented Nov 1, 2021

We should consider adding an API that enables the ability to invoke the parameter binding logic inherent in RequestDelegateFactory without having to create a RequestDelegate. This would be useful in situations where the default logic isn't invoked implicitly, e.g. when a complex parameter implements BindAsync, but invoking the behavior in an observable fashion would still add utility.

Such an API exists in the MinimalApis.Extensions library and is used by many of the included utility classes (example).

Strawman API proposal:

public static class RequestDelegateFactory
{
+    public static BindResult<TValue> BindParameterAsync<TValue>(HttpContext httpContext, ParameterInfo parameter);
}

+public class BindResult<TValue>
+{
+    public TValue? Value { get; }
+    public int? StatusCode { get; }
+    public void Deconstruct(out TValue? value, out int statusCode);
+}

Example use:

app.MapPost("/widgets", (Param<CreateWidget> widget, WidgetDb db) =>
{
    if (widget.AutoResponseStatusCode != 200)
    {
        // There was an issue binding, implement custom handling logic here
        return Results.Problem(statusCode: widget.AutoResponseStatusCode);
    }
    var newWidget = db.Insert(widget);
    return Results.Created($"/widgets/{newWidget.Id}", newWidget);
});

public class CreateWidget
{
    public string? Name { get; set; }
}

public class Param<TValue>
{
    public Param(TValue? value)
    {
        Value = value;
    }

    public TValue? Value { get; }
    public int AutoResponseStatusCode { get; init; } = 200;

    public static async ValueTask<Param<TValue?>> BindAsync(HttpContext context, ParameterInfo parameter)
    {
        var (defaultValue, statusCode) = await RequestDelegateFactory.BindParameterAsync(context, parameter);
        if (statusCode is not 200)
        {
            // The default logic would have implicitly returned due to an issue
            return new Param(null) { AutoResponseStatusCode = statusCode };
        }
        return new Param(defaultValue);
    }
}
@DamianEdwards DamianEdwards added enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Nov 1, 2021
@DamianEdwards DamianEdwards added this to the .NET 7 Planning milestone Nov 1, 2021
@rafikiassumani-msft rafikiassumani-msft added triage-focus Add this label to flag the issue for focus at triage Priority:3 Work that is nice to have Cost:L labels Jan 11, 2022
@amcasey amcasey added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Jun 2, 2023
@captainsafia captainsafia removed the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Jun 6, 2023
@captainsafia captainsafia modified the milestones: .NET 8 Planning, Backlog Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc enhancement This issue represents an ask for new feature or an enhancement to an existing one feature-minimal-actions Controller-like actions for endpoint routing Priority:3 Work that is nice to have triage-focus Add this label to flag the issue for focus at triage
Projects
None yet
Development

No branches or pull requests

4 participants