Open
Description
Background and Motivation
With the new QUERY verb reaching the Proposed Standard maturity level, now might be the right time to add support.
Proposed API
HttpQueryAttribute.cs
+ namespace Microsoft.AspNetCore.Mvc;
+
+ public class HttpQueryAttribute : HttpMethodAttribute
+ {
+ /// <summary>
+ /// Creates a new <see cref="HttpQueryAttribute"/>.
+ /// </summary>
+ public HttpQueryAttribute();
+
+ /// <summary>
+ /// Creates a new <see cref="HttpQueryAttribute"/> with the given route template.
+ /// </summary>
+ /// <param name="template">The route template. May not be null.</param>
+ public HttpQueryAttribute([StringSyntax("Route")] string template);
+ }
EndpointRouteBuilderExtensions
namespace Microsoft.AspNetCore.Builder;
+ public static class EndpointRouteBuilderExtensions
+ {
+ /// <summary>
+ /// Adds a <see cref="RouteEndpoint"/> to the <see cref="IEndpointRouteBuilder"/> that matches HTTP QUERY requests
+ /// for the specified pattern.
+ /// </summary>
+ /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add the route to.</param>
+ /// <param name="pattern">The route pattern.</param>
+ /// <param name="requestDelegate">The delegate executed when the endpoint is matched.</param>
+ /// <returns>A <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
+ public static IEndpointConventionBuilder MapQuery(
+ this IEndpointRouteBuilder endpoints,
+ [StringSyntax("Route")] string pattern,
+ RequestDelegate requestDelegate);
+
+ /// <summary>
+ /// Adds a <see cref="RouteEndpoint"/> to the <see cref="IEndpointRouteBuilder"/> that matches HTTP QUERY requests
+ /// for the specified pattern.
+ /// </summary>
+ /// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add the route to.</param>
+ /// <param name="pattern">The route pattern.</param>
+ /// <param name="handler">The delegate executed when the endpoint is matched.</param>
+ /// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
+ [RequiresUnreferencedCode(MapEndpointUnreferencedCodeWarning)]
+ [RequiresDynamicCode(MapEndpointDynamicCodeWarning)]
+ public static RouteHandlerBuilder MapQuery(
+ this IEndpointRouteBuilder endpoints,
+ [StringSyntax("Route")] string pattern,
+ Delegate handler);
+ }
HttpMethods
+ namespace Microsoft.AspNetCore.Http;
+
+ public static class HttpMethods
+ {
+ /// <summary>
+ /// HTTP "QUERY" method.
+ /// </summary>
+ public static readonly string Query = "QUERY";
+
+ /// <summary>
+ /// Returns a value that indicates if the HTTP request method is QUERY.
+ /// </summary>
+ /// <param name="method">The HTTP request method.</param>
+ /// <returns>
+ /// <see langword="true" /> if the method is QUERY; otherwise, <see langword="false" />.
+ /// </returns>
+ public static bool IsQuery(string method)
+ {
+ return Equals(Query, method);
+ }
+
+ public static string GetCanonicalizedValue(string method) => method switch
+ {
+ ...
+ string _ when IsQuery(method) => Query,
+ ...
+ };
+ }
Usage Examples
These are all intended to be used in the same way that their existing counterparts for the other HTTP methods are used.
[HttpQuery]
public async Task<ActionResult<TodoItem>> QueryForTodoItem(QueryParameters queryParameters)
{
...
}
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapQuery("/", queryParameters => dataSource.Search(queryParameters));
app.Run();