Skip to content

Commit 327292e

Browse files
committed
Add examples for a few Mvc attributes
1 parent 1d0f0d3 commit 327292e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ 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+
/// When placed on a parameter, the parameter will be bound to the value of the request header with the same name.
14+
/// Use the <see cref="Name"/> property to specify a different header name.
15+
/// </remarks>
16+
/// <example>
17+
/// In this example, the value of the 'User-Agent' header is bound to the parameter.
18+
/// <code>
19+
/// [HttpGet]
20+
/// public string GetUserAgent([FromHeader("User-Agent")] string userAgent)
21+
/// </code>
22+
/// Note that HTTP header names are case-insensitive, so the header name is matched without regard to case.
23+
/// </example>
1224
[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
1325
public class FromHeaderAttribute : Attribute, IBindingSourceMetadata, IModelNameProvider, IFromHeaderMetadata
1426
{

src/Mvc/Mvc.Core/src/ProducesResponseTypeAttribute.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,31 @@ namespace Microsoft.AspNetCore.Mvc;
1111
/// <summary>
1212
/// A filter that specifies the type of the value and status code returned by the action.
1313
/// </summary>
14+
/// <remarks>
15+
/// This attribute can be used to specify the type of the response body, the status code,
16+
/// and content-type(s) of the response.
17+
///
18+
/// In an MVC application, this attribute can be applied to an action method or controller class.
19+
/// In a Minimal API application, this attribute should be applied to the delegate method.
20+
///
21+
/// More than one instance of ProducesResponseTypeAttribute with different status codes may
22+
/// be used to specify multiple possible responses from an endpoint.
23+
/// </remarks>
24+
/// <example>
25+
/// In the following example, the ProducesResponseTypeAttribute is used to specify the type of the
26+
/// response body and the status code for the successful response and the status code, type, and content
27+
/// type for the error response.
28+
/// <code>
29+
/// app.MapGet("/todos/{id}",
30+
/// [ProducesResponseType(typeof(Todo), StatusCodes.Status200OK)]
31+
/// [ProducesResponseType(typeof(ProblemDetails), StatusCodes.Status404NotFound, "application/problem+json")]
32+
/// async (TodoDb db, string id) =>
33+
/// await db.Todos.FindAsync(id)
34+
/// is Todo todo
35+
/// ? Results.Ok(todo)
36+
/// : Results.NotFound());
37+
/// </code>
38+
/// </example>
1439
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
1540
public class ProducesResponseTypeAttribute : Attribute, IApiResponseMetadataProvider
1641
{

0 commit comments

Comments
 (0)