Skip to content

Add .ctor(string) to FromHeaderAttribute and FromQueryAttribute #36418

Open
@martincostello

Description

@martincostello

Background and Motivation

When using Minimal APIs, using explicit query string or header parameter names using attributes requires a less-minimal amount of attribute declaration that would be possible if the relevant attributes did not only have a default constructor.

Proposed API

namespace Microsoft.AspNetCore.Mvc
{
    public static class FromHeaderAttribute
    {
+       public FromHeaderAttribute(string name)
    }

    public static class FromQueryAttribute
    {
+       public FromQueryAttribute(string name)
    }

    public static class FromRouteAttribute
    {
+       public FromRouteAttribute(string name)
    }
}

Usage Examples

using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/search", (
    [FromQuery("q")] string? query,
    [FromHeader("User-Agent")] string? userAgent) =>
{
    return $@"You searched for ""{query}"" using ""{userAgent}"".";
});

app.Run();

Alternative Designs

None, retain the existing usage patterns.

using Microsoft.AspNetCore.Mvc;

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.MapGet("/search", (
    [FromQuery(Name = "q")] string? query,
    [FromHeader(Name = "User-Agent")] string? userAgent) =>
{
    return $@"You searched for ""{query}"" using ""{userAgent}"".";
});

app.Run();

Risks

None that I'm aware of.

EDIT: Added FromRouteAttribute too.

Activity

added
api-suggestionEarly API idea and discussion, it is NOT ready for implementation
on Sep 11, 2021
rafikiassumani-msft

rafikiassumani-msft commented on Sep 13, 2021

@rafikiassumani-msft
Contributor

Triage: We will evaluate this for .NET7

added
api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviews
on Oct 23, 2021
ghost

ghost commented on Oct 23, 2021

@ghost

Thank you for submitting this for API review. This will be reviewed by @dotnet/aspnet-api-review at the next meeting of the ASP.NET Core API Review group. Please ensure you take a look at the API review process documentation and ensure that:

  • The PR contains changes to the reference-assembly that describe the API change. Or, you have included a snippet of reference-assembly-style code that illustrates the API change.
  • The PR describes the impact to users, both positive (useful new APIs) and negative (breaking changes).
  • Someone is assigned to "champion" this change in the meeting, and they understand the impact and design of the change.
removed
api-suggestionEarly API idea and discussion, it is NOT ready for implementation
on Oct 23, 2021
added
api-suggestionEarly API idea and discussion, it is NOT ready for implementation
and removed
api-ready-for-reviewAPI is ready for formal API review - https://github.com/dotnet/apireviews
on Oct 25, 2021
pranavkm

pranavkm commented on Oct 25, 2021

@pranavkm
Contributor

API review:
@martincostello while we think this API proposal is valid, we want to wait and see given a) a solution exists using the property (b) we might want to do something to not use MVC attributes with route handlers. We'll put this in our backlog for now and bring this back up part way thru 7.0 once we have had a better sense for how these attributes get used.

ghost

ghost commented on Jun 14, 2022

@ghost

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

added
area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
on Jun 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routingtriage-focusAdd this label to flag the issue for focus at triage

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @davidfowl@pranavkm@martincostello@captainsafia@amcasey

        Issue actions

          Add .ctor(string) to FromHeaderAttribute and FromQueryAttribute · Issue #36418 · dotnet/aspnetcore