Skip to content

Commit 9100687

Browse files
committed
Moved new section to after BindAsync and added to intro
1 parent e00f1f8 commit 9100687

4 files changed

Lines changed: 112 additions & 108 deletions

File tree

aspnetcore/fundamentals/minimal-apis/includes/parameter-binding10.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,11 @@ The [complete sample code](https://github.com/dotnet/AspNetCore.Docs.Samples/tre
321321

322322
### Custom Binding
323323

324-
There are two ways to customize parameter binding:
324+
There are three ways to customize parameter binding:
325325

326326
1. For route, query, and header binding sources, bind custom types by adding a static `TryParse` method for the type.
327327
1. Control the binding process by implementing a `BindAsync` method on a type.
328+
1. For advanced scenarios, implement the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface to provide custom binding logic directly from the `HttpContext`.
328329

329330
#### TryParse
330331

@@ -354,6 +355,32 @@ The following code displays `SortBy:xyz, SortDirection:Desc, CurrentPage:99` wit
354355

355356
<a name="bf"></a>
356357

358+
#### Custom parameter binding with `IBindableFromHttpContext`
359+
360+
ASP.NET Core provides support for custom parameter binding in Minimal APIs using the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface. This interface, introduced with C# 11's static abstract members, allows you to create types that can be bound from an HTTP context directly in route handler parameters.
361+
362+
```csharp
363+
public interface IBindableFromHttpContext<TSelf>
364+
where TSelf : class, IBindableFromHttpContext<TSelf>
365+
{
366+
static abstract ValueTask<TSelf?> BindAsync(HttpContext context, ParameterInfo parameter);
367+
}
368+
```
369+
370+
By implementing the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601>, you can create custom types that handle their own binding logic from the HttpContext. When a route handler includes a parameter of this type, the framework automatically calls the static BindAsync method to create the instance:
371+
372+
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
373+
374+
The following is an example implementation of a custom parameter that binds from an HTTP header:
375+
376+
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/CustomBoundParameters.cs":::
377+
378+
You can also implement validation within your custom binding logic:
379+
380+
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
381+
382+
[View or download the sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/minimal-apis/10.0-samples/CustomBindingExample) ([how to download](xref:index#how-to-download-a-sample))
383+
357384
### Binding failures
358385

359386
When binding fails, the framework logs a debug message and returns various status codes to the client depending on the failure mode.
@@ -396,32 +423,6 @@ The rules for determining a binding source from a parameter:
396423
1. If the parameter type is a service provided by dependency injection, it uses that service as the source.
397424
1. The parameter is from the body.
398425

399-
### Custom parameter binding with `IBindableFromHttpContext`
400-
401-
ASP.NET Core provides support for custom parameter binding in Minimal APIs using the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface. This interface, introduced with C# 11's static abstract members, allows you to create types that can be bound from an HTTP context directly in route handler parameters.
402-
403-
```csharp
404-
public interface IBindableFromHttpContext<TSelf>
405-
where TSelf : class, IBindableFromHttpContext<TSelf>
406-
{
407-
static abstract ValueTask<TSelf?> BindAsync(HttpContext context, ParameterInfo parameter);
408-
}
409-
```
410-
411-
By implementing the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601>, you can create custom types that handle their own binding logic from the HttpContext. When a route handler includes a parameter of this type, the framework automatically calls the static BindAsync method to create the instance:
412-
413-
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
414-
415-
The following is an example implementation of a custom parameter that binds from an HTTP header:
416-
417-
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/CustomBoundParameters.cs":::
418-
419-
You can also implement validation within your custom binding logic:
420-
421-
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
422-
423-
[View or download the sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/minimal-apis/10.0-samples/CustomBindingExample) ([how to download](xref:index#how-to-download-a-sample))
424-
425426
### Configure JSON deserialization options for body binding
426427

427428
The body binding source uses <xref:System.Text.Json?displayProperty=fullName> for deserialization. It is ***not*** possible to change this default, but JSON serialization and deserialization options can be configured.

aspnetcore/fundamentals/minimal-apis/includes/parameter-binding7.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ The [complete sample code](https://github.com/dotnet/AspNetCore.Docs.Samples/tre
249249

250250
### Custom Binding
251251

252-
There are two ways to customize parameter binding:
252+
There are three ways to customize parameter binding:
253253

254254
1. For route, query, and header binding sources, bind custom types by adding a static `TryParse` method for the type.
255255
1. Control the binding process by implementing a `BindAsync` method on a type.
256+
1. For advanced scenarios, implement the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface to provide custom binding logic directly from the `HttpContext`.
256257

257258
#### TryParse
258259

@@ -282,6 +283,32 @@ The following code displays `SortBy:xyz, SortDirection:Desc, CurrentPage:99` wit
282283

283284
<a name="bf"></a>
284285

286+
#### Custom parameter binding with `IBindableFromHttpContext`
287+
288+
ASP.NET Core provides support for custom parameter binding in Minimal APIs using the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface. This interface, introduced with C# 11's static abstract members, allows you to create types that can be bound from an HTTP context directly in route handler parameters.
289+
290+
```csharp
291+
public interface IBindableFromHttpContext<TSelf>
292+
where TSelf : class, IBindableFromHttpContext<TSelf>
293+
{
294+
static abstract ValueTask<TSelf?> BindAsync(HttpContext context, ParameterInfo parameter);
295+
}
296+
```
297+
298+
By implementing the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601>, you can create custom types that handle their own binding logic from the HttpContext. When a route handler includes a parameter of this type, the framework automatically calls the static BindAsync method to create the instance:
299+
300+
:::code language="csharp" source="~/fundamentals/minimal-apis/7.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
301+
302+
The following is an example implementation of a custom parameter that binds from an HTTP header:
303+
304+
:::code language="csharp" source="~/fundamentals/minimal-apis/7.0-samples/CustomBindingExample/CustomBoundParameter.cs":::
305+
306+
You can also implement validation within your custom binding logic:
307+
308+
:::code language="csharp" source="~/fundamentals/minimal-apis/7.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
309+
310+
[View or download the sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/minimal-apis/7.0-samples/CustomBindingExample) ([how to download](xref:index#how-to-download-a-sample))
311+
285312
### Binding failures
286313

287314
When binding fails, the framework logs a debug message and returns various status codes to the client depending on the failure mode.
@@ -322,32 +349,6 @@ The rules for determining a binding source from a parameter:
322349
1. If the parameter type is a service provided by dependency injection, it uses that service as the source.
323350
1. The parameter is from the body.
324351

325-
### Custom parameter binding with `IBindableFromHttpContext`
326-
327-
ASP.NET Core provides support for custom parameter binding in Minimal APIs using the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface. This interface, introduced with C# 11's static abstract members, allows you to create types that can be bound from an HTTP context directly in route handler parameters.
328-
329-
```csharp
330-
public interface IBindableFromHttpContext<TSelf>
331-
where TSelf : class, IBindableFromHttpContext<TSelf>
332-
{
333-
static abstract ValueTask<TSelf?> BindAsync(HttpContext context, ParameterInfo parameter);
334-
}
335-
```
336-
337-
By implementing the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601>, you can create custom types that handle their own binding logic from the HttpContext. When a route handler includes a parameter of this type, the framework automatically calls the static BindAsync method to create the instance:
338-
339-
:::code language="csharp" source="~/fundamentals/minimal-apis/7.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
340-
341-
The following is an example implementation of a custom parameter that binds from an HTTP header:
342-
343-
:::code language="csharp" source="~/fundamentals/minimal-apis/7.0-samples/CustomBindingExample/CustomBoundParameter.cs":::
344-
345-
You can also implement validation within your custom binding logic:
346-
347-
:::code language="csharp" source="~/fundamentals/minimal-apis/7.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
348-
349-
[View or download the sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/minimal-apis/7.0-samples/CustomBindingExample) ([how to download](xref:index#how-to-download-a-sample))
350-
351352
### Configure JSON deserialization options for body binding
352353

353354
The body binding source uses <xref:System.Text.Json?displayProperty=fullName> for deserialization. It is ***not*** possible to change this default, but JSON serialization and deserialization options can be configured.

aspnetcore/fundamentals/minimal-apis/includes/parameter-binding8.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,11 @@ The [complete sample code](https://github.com/dotnet/AspNetCore.Docs.Samples/tre
321321

322322
### Custom Binding
323323

324-
There are two ways to customize parameter binding:
324+
There are three ways to customize parameter binding:
325325

326326
1. For route, query, and header binding sources, bind custom types by adding a static `TryParse` method for the type.
327327
1. Control the binding process by implementing a `BindAsync` method on a type.
328+
1. For advanced scenarios, implement the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface to provide custom binding logic directly from the `HttpContext`.
328329

329330
#### TryParse
330331

@@ -354,6 +355,32 @@ The following code displays `SortBy:xyz, SortDirection:Desc, CurrentPage:99` wit
354355

355356
<a name="bf"></a>
356357

358+
#### Custom parameter binding with `IBindableFromHttpContext`
359+
360+
ASP.NET Core provides support for custom parameter binding in Minimal APIs using the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface. This interface, introduced with C# 11's static abstract members, allows you to create types that can be bound from an HTTP context directly in route handler parameters.
361+
362+
```csharp
363+
public interface IBindableFromHttpContext<TSelf>
364+
where TSelf : class, IBindableFromHttpContext<TSelf>
365+
{
366+
static abstract ValueTask<TSelf?> BindAsync(HttpContext context, ParameterInfo parameter);
367+
}
368+
```
369+
370+
By implementing the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601>, you can create custom types that handle their own binding logic from the HttpContext. When a route handler includes a parameter of this type, the framework automatically calls the static BindAsync method to create the instance:
371+
372+
:::code language="csharp" source="~/fundamentals/minimal-apis/8.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
373+
374+
The following is an example implementation of a custom parameter that binds from an HTTP header:
375+
376+
:::code language="csharp" source="~/fundamentals/minimal-apis/8.0-samples/CustomBindingExample/CustomBoundParameter.cs":::
377+
378+
You can also implement validation within your custom binding logic:
379+
380+
:::code language="csharp" source="~/fundamentals/minimal-apis/8.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
381+
382+
[View or download the sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/minimal-apis/8.0-samples/CustomBindingExample) ([how to download](xref:index#how-to-download-a-sample))
383+
357384
### Binding failures
358385

359386
When binding fails, the framework logs a debug message and returns various status codes to the client depending on the failure mode.
@@ -396,32 +423,6 @@ The rules for determining a binding source from a parameter:
396423
1. If the parameter type is a service provided by dependency injection, it uses that service as the source.
397424
1. The parameter is from the body.
398425

399-
### Custom parameter binding with `IBindableFromHttpContext`
400-
401-
ASP.NET Core provides support for custom parameter binding in Minimal APIs using the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface. This interface, introduced with C# 11's static abstract members, allows you to create types that can be bound from an HTTP context directly in route handler parameters.
402-
403-
```csharp
404-
public interface IBindableFromHttpContext<TSelf>
405-
where TSelf : class, IBindableFromHttpContext<TSelf>
406-
{
407-
static abstract ValueTask<TSelf?> BindAsync(HttpContext context, ParameterInfo parameter);
408-
}
409-
```
410-
411-
By implementing the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601>, you can create custom types that handle their own binding logic from the HttpContext. When a route handler includes a parameter of this type, the framework automatically calls the static BindAsync method to create the instance:
412-
413-
:::code language="csharp" source="~/fundamentals/minimal-apis/8.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
414-
415-
The following is an example implementation of a custom parameter that binds from an HTTP header:
416-
417-
:::code language="csharp" source="~/fundamentals/minimal-apis/8.0-samples/CustomBindingExample/CustomBoundParameter.cs":::
418-
419-
You can also implement validation within your custom binding logic:
420-
421-
:::code language="csharp" source="~/fundamentals/minimal-apis/8.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
422-
423-
[View or download the sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/minimal-apis/8.0-samples/CustomBindingExample) ([how to download](xref:index#how-to-download-a-sample))
424-
425426
### Configure JSON deserialization options for body binding
426427

427428
The body binding source uses <xref:System.Text.Json?displayProperty=fullName> for deserialization. It is ***not*** possible to change this default, but JSON serialization and deserialization options can be configured.

aspnetcore/fundamentals/minimal-apis/includes/parameter-binding9.md

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,11 @@ The [complete sample code](https://github.com/dotnet/AspNetCore.Docs.Samples/tre
321321

322322
### Custom Binding
323323

324-
There are two ways to customize parameter binding:
324+
There are three ways to customize parameter binding:
325325

326326
1. For route, query, and header binding sources, bind custom types by adding a static `TryParse` method for the type.
327327
1. Control the binding process by implementing a `BindAsync` method on a type.
328+
1. For advanced scenarios, implement the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface to provide custom binding logic directly from the `HttpContext`.
328329

329330
#### TryParse
330331

@@ -354,6 +355,32 @@ The following code displays `SortBy:xyz, SortDirection:Desc, CurrentPage:99` wit
354355

355356
<a name="bf"></a>
356357

358+
#### Custom parameter binding with `IBindableFromHttpContext`
359+
360+
ASP.NET Core provides support for custom parameter binding in Minimal APIs using the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface. This interface, introduced with C# 11's static abstract members, allows you to create types that can be bound from an HTTP context directly in route handler parameters.
361+
362+
```csharp
363+
public interface IBindableFromHttpContext<TSelf>
364+
where TSelf : class, IBindableFromHttpContext<TSelf>
365+
{
366+
static abstract ValueTask<TSelf?> BindAsync(HttpContext context, ParameterInfo parameter);
367+
}
368+
```
369+
370+
By implementing the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601>, you can create custom types that handle their own binding logic from the HttpContext. When a route handler includes a parameter of this type, the framework automatically calls the static BindAsync method to create the instance:
371+
372+
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
373+
374+
The following is an example implementation of a custom parameter that binds from an HTTP header:
375+
376+
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/CustomBindingExample/CustomBoundParameters.cs":::
377+
378+
You can also implement validation within your custom binding logic:
379+
380+
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
381+
382+
[View or download the sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/minimal-apis/9.0-samples/CustomBindingExample) ([how to download](xref:index#how-to-download-a-sample))
383+
357384
### Binding failures
358385

359386
When binding fails, the framework logs a debug message and returns various status codes to the client depending on the failure mode.
@@ -396,32 +423,6 @@ The rules for determining a binding source from a parameter:
396423
1. If the parameter type is a service provided by dependency injection, it uses that service as the source.
397424
1. The parameter is from the body.
398425

399-
### Custom parameter binding with `IBindableFromHttpContext`
400-
401-
ASP.NET Core provides support for custom parameter binding in Minimal APIs using the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601> interface. This interface, introduced with C# 11's static abstract members, allows you to create types that can be bound from an HTTP context directly in route handler parameters.
402-
403-
```csharp
404-
public interface IBindableFromHttpContext<TSelf>
405-
where TSelf : class, IBindableFromHttpContext<TSelf>
406-
{
407-
static abstract ValueTask<TSelf?> BindAsync(HttpContext context, ParameterInfo parameter);
408-
}
409-
```
410-
411-
By implementing the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601>, you can create custom types that handle their own binding logic from the HttpContext. When a route handler includes a parameter of this type, the framework automatically calls the static BindAsync method to create the instance:
412-
413-
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
414-
415-
The following is an example implementation of a custom parameter that binds from an HTTP header:
416-
417-
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/CustomBindingExample/CustomBoundParameters.cs":::
418-
419-
You can also implement validation within your custom binding logic:
420-
421-
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
422-
423-
[View or download the sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/fundamentals/minimal-apis/9.0-samples/CustomBindingExample) ([how to download](xref:index#how-to-download-a-sample))
424-
425426
### Configure JSON deserialization options for body binding
426427

427428
The body binding source uses <xref:System.Text.Json?displayProperty=fullName> for deserialization. It is ***not*** possible to change this default, but JSON serialization and deserialization options can be configured.

0 commit comments

Comments
 (0)