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.

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

    Issue actions