Skip to content

Commit

Permalink
Add examples for attributes (#58196)
Browse files Browse the repository at this point in the history
* Add examples for a few Mvc attributes

* Scope to parameter binding attributes.

* Fixes from PR review
  • Loading branch information
mikekistler authored Oct 30, 2024
1 parent 4dab764 commit 3f8edf1
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Mvc/Mvc.Core/src/FromBodyAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,33 @@ namespace Microsoft.AspNetCore.Mvc;
/// Specifies that a parameter or property should be bound using the request body.
/// </summary>
/// <remarks>
/// Binds a parameter or property to the request body.
///
/// By default, ASP.NET Core MVC delegates the responsibility of reading the body to an input formatter.<br/>
/// In the case of ASP.NET Core Minimal APIs, the body is deserialized by <see cref="System.Text.Json.JsonSerializer"/>.
///
/// For more information about parameter binding see
/// <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis/parameter-binding">Parameter binding</see>.
/// </remarks>
/// <example>
/// In this example, the value of the 'name' field in the form-data request body is bound to the name parameter,
/// and the value of the 'age' field is bound to the age parameter.
/// <code>
/// app.MapPost("/from-body", ([FromBody] Person person) => person);
///
/// public record Person(string Name, int Age);
/// </code>
///
/// If the EmptyBodyBehavior is set to EmptyBodyBehavior.Allow in the FromBody attribute,
/// requests without a request body are allowed.
/// <code>
/// app.MapPost("/allow-empty-body",
/// (
/// [Description("An optional request body")]
/// [FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)] Body body
/// ) =>
/// </code>
/// </example>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FromBodyAttribute : Attribute, IBindingSourceMetadata, IConfigureEmptyBodyBehavior, IFromBodyMetadata
{
Expand Down
16 changes: 16 additions & 0 deletions src/Mvc/Mvc.Core/src/FromFormAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ namespace Microsoft.AspNetCore.Mvc;
/// <summary>
/// Specifies that a parameter or property should be bound using form-data in the request body.
/// </summary>
/// <remarks>
/// Binds a parameter or property to a field in a form-data request body with the same name,
/// or the name specified in the <see cref="Name"/> property.
/// Form parameter names are matched case-insensitively.
///
/// For more information about parameter binding see
/// <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis/parameter-binding">Parameter binding</see>.
/// </remarks>
/// <example>
/// In this example, the value of the 'name' field in the form-data request body is bound to the name parameter,
/// and the value of the 'age' field is bound to the age parameter.
/// <code>
/// app.MapPost("/from-form", ([FromForm] string name, [FromForm] int age)
/// => new { Name = name, Age = age });
/// </code>
/// </example>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FromFormAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromFormMetadata
{
Expand Down
15 changes: 15 additions & 0 deletions src/Mvc/Mvc.Core/src/FromHeaderAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ namespace Microsoft.AspNetCore.Mvc;
/// <summary>
/// Specifies that a parameter or property should be bound using the request headers.
/// </summary>
/// <remarks>
/// Binds a parameter or property to the value of the request header with the same name,
/// or the name specified in the <see cref="Name"/> property.
/// Note that HTTP header names are case-insensitive, so the header name is matched without regard to case.
///
/// For more information about parameter binding see
/// <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis/parameter-binding">Parameter binding</see>.
/// </remarks>
/// <example>
/// In this example, the value of the 'User-Agent' header is bound to the parameter.
/// <code>
/// app.MapGet("/user-agent", ([FromHeader(Name = "User-Agent")] string userAgent)
/// => userAgent);
/// </code>
/// </example>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FromHeaderAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromHeaderMetadata
{
Expand Down
15 changes: 15 additions & 0 deletions src/Mvc/Mvc.Core/src/FromQueryAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ namespace Microsoft.AspNetCore.Mvc;
/// <summary>
/// Specifies that a parameter or property should be bound using the request query string.
/// </summary>
/// <remarks>
/// Binds a parameter or property to the value of query string parameter with the same name,
/// or the name specified in the <see cref="Name"/> property.
/// The query parameter name is matched case-insensitively.
///
/// For more information about parameter binding see
/// <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis/parameter-binding">Parameter binding</see>.
/// </remarks>
/// <example>
/// In this example, the value of the 'User-Agent' header is bound to the parameter.
/// <code>
/// app.MapGet("/version", ([FromQuery(Name = "api-version")] string apiVersion)
/// => apiVersion);
/// </code>
/// </example>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FromQueryAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromQueryMetadata
{
Expand Down
14 changes: 14 additions & 0 deletions src/Mvc/Mvc.Core/src/FromRouteAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ namespace Microsoft.AspNetCore.Mvc;
/// <summary>
/// Specifies that a parameter or property should be bound using route-data from the current request.
/// </summary>
/// <remarks>
/// Binds a parameter or property to the value of a route parameter with the same name,
/// or the name specified in the <see cref="Name"/> property.
/// The route parameter name is matched against parameter segments of the route pattern case-insensitively.
///
/// For more information about parameter binding see
/// <see href="https://learn.microsoft.com/aspnet/core/fundamentals/minimal-apis/parameter-binding">Parameter binding</see>.
/// </remarks>
/// <example>
/// In this example, the value of the 'id' route parameter is bound to the parameter.
/// <code>
/// app.MapGet("/from-route/{id}", ([FromRoute] string id) => id);
/// </code>
/// </example>
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FromRouteAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromRouteMetadata
{
Expand Down

0 comments on commit 3f8edf1

Please sign in to comment.