Skip to content

Commit 3f8edf1

Browse files
authored
Add examples for attributes (#58196)
* Add examples for a few Mvc attributes * Scope to parameter binding attributes. * Fixes from PR review
1 parent 4dab764 commit 3f8edf1

File tree

5 files changed

+84
-0
lines changed

5 files changed

+84
-0
lines changed

src/Mvc/Mvc.Core/src/FromBodyAttribute.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,33 @@ namespace Microsoft.AspNetCore.Mvc;
1010
/// Specifies that a parameter or property should be bound using the request body.
1111
/// </summary>
1212
/// <remarks>
13+
/// Binds a parameter or property to the request body.
14+
///
1315
/// By default, ASP.NET Core MVC delegates the responsibility of reading the body to an input formatter.<br/>
1416
/// In the case of ASP.NET Core Minimal APIs, the body is deserialized by <see cref="System.Text.Json.JsonSerializer"/>.
17+
///
18+
/// For more information about parameter binding see
19+
/// <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis/parameter-binding">Parameter binding</see>.
1520
/// </remarks>
21+
/// <example>
22+
/// In this example, the value of the 'name' field in the form-data request body is bound to the name parameter,
23+
/// and the value of the 'age' field is bound to the age parameter.
24+
/// <code>
25+
/// app.MapPost("/from-body", ([FromBody] Person person) => person);
26+
///
27+
/// public record Person(string Name, int Age);
28+
/// </code>
29+
///
30+
/// If the EmptyBodyBehavior is set to EmptyBodyBehavior.Allow in the FromBody attribute,
31+
/// requests without a request body are allowed.
32+
/// <code>
33+
/// app.MapPost("/allow-empty-body",
34+
/// (
35+
/// [Description("An optional request body")]
36+
/// [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] Body body
37+
/// ) =>
38+
/// </code>
39+
/// </example>
1640
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
1741
public class FromBodyAttribute : Attribute, IBindingSourceMetadata, IConfigureEmptyBodyBehavior, IFromBodyMetadata
1842
{

src/Mvc/Mvc.Core/src/FromFormAttribute.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ namespace Microsoft.AspNetCore.Mvc;
99
/// <summary>
1010
/// Specifies that a parameter or property should be bound using form-data in the request body.
1111
/// </summary>
12+
/// <remarks>
13+
/// Binds a parameter or property to a field in a form-data request body with the same name,
14+
/// or the name specified in the <see cref="Name"/> property.
15+
/// Form parameter names are matched case-insensitively.
16+
///
17+
/// For more information about parameter binding see
18+
/// <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis/parameter-binding">Parameter binding</see>.
19+
/// </remarks>
20+
/// <example>
21+
/// In this example, the value of the 'name' field in the form-data request body is bound to the name parameter,
22+
/// and the value of the 'age' field is bound to the age parameter.
23+
/// <code>
24+
/// app.MapPost("/from-form", ([FromForm] string name, [FromForm] int age)
25+
/// => new { Name = name, Age = age });
26+
/// </code>
27+
/// </example>
1228
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
1329
public class FromFormAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromFormMetadata
1430
{

src/Mvc/Mvc.Core/src/FromHeaderAttribute.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ namespace Microsoft.AspNetCore.Mvc;
99
/// <summary>
1010
/// Specifies that a parameter or property should be bound using the request headers.
1111
/// </summary>
12+
/// <remarks>
13+
/// Binds a parameter or property to the value of the request header with the same name,
14+
/// or the name specified in the <see cref="Name"/> property.
15+
/// Note that HTTP header names are case-insensitive, so the header name is matched without regard to case.
16+
///
17+
/// For more information about parameter binding see
18+
/// <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis/parameter-binding">Parameter binding</see>.
19+
/// </remarks>
20+
/// <example>
21+
/// In this example, the value of the 'User-Agent' header is bound to the parameter.
22+
/// <code>
23+
/// app.MapGet("/user-agent", ([FromHeader(Name = "User-Agent")] string userAgent)
24+
/// => userAgent);
25+
/// </code>
26+
/// </example>
1227
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
1328
public class FromHeaderAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromHeaderMetadata
1429
{

src/Mvc/Mvc.Core/src/FromQueryAttribute.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,21 @@ namespace Microsoft.AspNetCore.Mvc;
99
/// <summary>
1010
/// Specifies that a parameter or property should be bound using the request query string.
1111
/// </summary>
12+
/// <remarks>
13+
/// Binds a parameter or property to the value of query string parameter with the same name,
14+
/// or the name specified in the <see cref="Name"/> property.
15+
/// The query parameter name is matched case-insensitively.
16+
///
17+
/// For more information about parameter binding see
18+
/// <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis/parameter-binding">Parameter binding</see>.
19+
/// </remarks>
20+
/// <example>
21+
/// In this example, the value of the 'User-Agent' header is bound to the parameter.
22+
/// <code>
23+
/// app.MapGet("/version", ([FromQuery(Name = "api-version")] string apiVersion)
24+
/// => apiVersion);
25+
/// </code>
26+
/// </example>
1227
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
1328
public class FromQueryAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromQueryMetadata
1429
{

src/Mvc/Mvc.Core/src/FromRouteAttribute.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ namespace Microsoft.AspNetCore.Mvc;
1010
/// <summary>
1111
/// Specifies that a parameter or property should be bound using route-data from the current request.
1212
/// </summary>
13+
/// <remarks>
14+
/// Binds a parameter or property to the value of a route parameter with the same name,
15+
/// or the name specified in the <see cref="Name"/> property.
16+
/// The route parameter name is matched against parameter segments of the route pattern case-insensitively.
17+
///
18+
/// For more information about parameter binding see
19+
/// <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis/parameter-binding">Parameter binding</see>.
20+
/// </remarks>
21+
/// <example>
22+
/// In this example, the value of the 'id' route parameter is bound to the parameter.
23+
/// <code>
24+
/// app.MapGet("/from-route/{id}", ([FromRoute] string id) => id);
25+
/// </code>
26+
/// </example>
1327
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
1428
public class FromRouteAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromRouteMetadata
1529
{

0 commit comments

Comments
 (0)