Skip to content

Commit af2e23c

Browse files
Add comprehensive documentation and examples for HTTP QUERY method
- Create detailed usage examples for HttpMethods.Query - Show MVC controller examples with HttpQueryAttribute - Demonstrate minimal API usage with MapQuery - Include HTTP client examples - Document benefits and use cases of QUERY method - All functionality fully implemented and tested Co-authored-by: BrennanConroy <[email protected]>
1 parent 5ff683b commit af2e23c

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed

HTTP_QUERY_USAGE_EXAMPLES.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# HTTP QUERY Method Support in ASP.NET Core
2+
3+
This document provides examples of how to use the new HTTP QUERY method support added to ASP.NET Core.
4+
5+
## HttpMethods Class
6+
7+
The `HttpMethods` class now includes support for the QUERY method:
8+
9+
```csharp
10+
using Microsoft.AspNetCore.Http;
11+
12+
// New QUERY method constant
13+
string queryMethod = HttpMethods.Query; // "QUERY"
14+
15+
// New IsQuery method
16+
bool isQuery = HttpMethods.IsQuery("QUERY"); // true
17+
bool isQueryLowercase = HttpMethods.IsQuery("query"); // true
18+
19+
// GetCanonicalizedValue now supports QUERY
20+
string canonicalized = HttpMethods.GetCanonicalizedValue("query"); // Returns HttpMethods.Query
21+
```
22+
23+
## MVC Controller Support
24+
25+
Use the new `HttpQueryAttribute` in MVC controllers:
26+
27+
```csharp
28+
using Microsoft.AspNetCore.Mvc;
29+
30+
[ApiController]
31+
[Route("api/[controller]")]
32+
public class DataController : ControllerBase
33+
{
34+
[HttpQuery]
35+
public IActionResult Search([FromQuery] string q, [FromQuery] int limit = 10)
36+
{
37+
// Handle QUERY request with query parameters in the body
38+
return Ok(new { query = q, limit = limit });
39+
}
40+
41+
[HttpQuery("search/{category}")]
42+
public IActionResult SearchByCategory(string category, [FromQuery] string q)
43+
{
44+
// Handle QUERY request with both route and query parameters
45+
return Ok(new { category = category, query = q });
46+
}
47+
}
48+
```
49+
50+
## Minimal API Support
51+
52+
Use the new `MapQuery` extension methods with minimal APIs:
53+
54+
```csharp
55+
using Microsoft.AspNetCore.Http;
56+
57+
var builder = WebApplication.CreateBuilder(args);
58+
var app = builder.Build();
59+
60+
// MapQuery with RequestDelegate
61+
app.MapQuery("/search", async (HttpContext context) =>
62+
{
63+
var query = context.Request.Query["q"].ToString();
64+
return Results.Ok(new { query = query });
65+
});
66+
67+
// MapQuery with typed delegate
68+
app.MapQuery("/search/{category}", (string category, string q) =>
69+
{
70+
return Results.Ok(new { category = category, query = q });
71+
});
72+
73+
// MapQuery with async delegate
74+
app.MapQuery("/async-search", async (string q, ILogger<Program> logger) =>
75+
{
76+
logger.LogInformation("Processing query: {Query}", q);
77+
await Task.Delay(100); // Simulate async work
78+
return Results.Ok(new { query = q, timestamp = DateTime.UtcNow });
79+
});
80+
81+
app.Run();
82+
```
83+
84+
## Usage with HTTP Clients
85+
86+
Example of making QUERY requests:
87+
88+
```csharp
89+
using System.Net.Http;
90+
91+
var client = new HttpClient();
92+
93+
// Simple QUERY request
94+
var request = new HttpRequestMessage(new HttpMethod("QUERY"), "https://api.example.com/search?q=test");
95+
var response = await client.SendAsync(request);
96+
97+
// QUERY request with content body (if supported by the API)
98+
var queryRequest = new HttpRequestMessage(new HttpMethod("QUERY"), "https://api.example.com/advanced-search")
99+
{
100+
Content = JsonContent.Create(new {
101+
query = "search terms",
102+
filters = new { category = "books", minPrice = 10 }
103+
})
104+
};
105+
var queryResponse = await client.SendAsync(queryRequest);
106+
```
107+
108+
## Benefits of HTTP QUERY Method
109+
110+
The HTTP QUERY method is designed for safe queries that may include request bodies:
111+
112+
- **Safe**: Like GET, QUERY requests should not modify server state
113+
- **Cacheable**: QUERY responses can be cached like GET responses
114+
- **Request Body**: Unlike GET, QUERY allows request bodies for complex query parameters
115+
- **Semantic Clarity**: Clearly indicates query operations vs. data retrieval (GET)
116+
117+
For more information, see the HTTP QUERY method specification: https://datatracker.ietf.org/doc/draft-ietf-httpbis-safe-method-w-body/

0 commit comments

Comments
 (0)