Skip to content

Commit ddb5c23

Browse files
committed
Added more code samples and intros
1 parent 2b10969 commit ddb5c23

7 files changed

Lines changed: 41 additions & 29 deletions

File tree

aspnetcore/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/Program.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77

88
app.UseHttpsRedirection();
99

10-
// Basic endpoint to test the application
1110
app.MapGet("/", () => "Hello, IBindableFromHttpContext example!");
1211

13-
// Endpoint using the CustomBoundParameter
1412
app.MapGet("/custom-binding", (CustomBoundParameter param) =>
1513
{
1614
return $"Value from custom binding: {param.Value}";
1715
});
1816

19-
// Endpoint showing how to combine with other parameters
2017
app.MapGet("/combined/{id}", (int id, CustomBoundParameter param) =>
2118
{
2219
return $"ID: {id}, Custom Value: {param.Value}";
2320
});
24-
25-
// Endpoint showing validation example
21+
// </snippet_IBindableFromHttpContext>
22+
// <snippet_validation>
2623
app.MapGet("/validated", (ValidatedParameter param) =>
2724
{
2825
if (string.IsNullOrEmpty(param.Value))
@@ -32,6 +29,6 @@
3229

3330
return Results.Ok($"Validated value: {param.Value}");
3431
});
35-
// <//snippet_IBindableFromHttpContext>
32+
// </snippet_validation>
3633

3734
app.Run();

aspnetcore/fundamentals/minimal-apis/7.0-samples/CustomBindingExample/Program.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77

88
app.UseHttpsRedirection();
99

10-
// Basic endpoint to test the application
1110
app.MapGet("/", () => "Hello, IBindableFromHttpContext example!");
1211

13-
// Endpoint using the CustomBoundParameter
1412
app.MapGet("/custom-binding", (CustomBoundParameter param) =>
1513
{
1614
return $"Value from custom binding: {param.Value}";
1715
});
1816

19-
// Endpoint showing how to combine with other parameters
2017
app.MapGet("/combined/{id}", (int id, CustomBoundParameter param) =>
2118
{
2219
return $"ID: {id}, Custom Value: {param.Value}";
2320
});
24-
25-
// Endpoint showing validation example
21+
// </snippet_IBindableFromHttpContext>
22+
// <snippet_validation>
2623
app.MapGet("/validated", (ValidatedParameter param) =>
2724
{
2825
if (string.IsNullOrEmpty(param.Value))
@@ -32,6 +29,6 @@
3229

3330
return Results.Ok($"Validated value: {param.Value}");
3431
});
35-
// <//snippet_IBindableFromHttpContext>
32+
// </snippet_validation>
3633

3734
app.Run();

aspnetcore/fundamentals/minimal-apis/8.0-samples/CustomBindingExample/Program.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77

88
app.UseHttpsRedirection();
99

10-
// Basic endpoint to test the application
1110
app.MapGet("/", () => "Hello, IBindableFromHttpContext example!");
1211

13-
// Endpoint using the CustomBoundParameter
1412
app.MapGet("/custom-binding", (CustomBoundParameter param) =>
1513
{
1614
return $"Value from custom binding: {param.Value}";
1715
});
1816

19-
// Endpoint showing how to combine with other parameters
2017
app.MapGet("/combined/{id}", (int id, CustomBoundParameter param) =>
2118
{
2219
return $"ID: {id}, Custom Value: {param.Value}";
2320
});
24-
25-
// Endpoint showing validation example
21+
// </snippet_IBindableFromHttpContext>
22+
// <snippet_validation>
2623
app.MapGet("/validated", (ValidatedParameter param) =>
2724
{
2825
if (string.IsNullOrEmpty(param.Value))
@@ -32,6 +29,6 @@
3229

3330
return Results.Ok($"Validated value: {param.Value}");
3431
});
35-
// <//snippet_IBindableFromHttpContext>
32+
// </snippet_validation>
3633

3734
app.Run();

aspnetcore/fundamentals/minimal-apis/9.0-samples/CustomBindingExample/Program.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,19 @@
77

88
app.UseHttpsRedirection();
99

10-
// Basic endpoint to test the application
1110
app.MapGet("/", () => "Hello, IBindableFromHttpContext example!");
1211

13-
// Endpoint using the CustomBoundParameter
1412
app.MapGet("/custom-binding", (CustomBoundParameter param) =>
1513
{
1614
return $"Value from custom binding: {param.Value}";
1715
});
1816

19-
// Endpoint showing how to combine with other parameters
2017
app.MapGet("/combined/{id}", (int id, CustomBoundParameter param) =>
2118
{
2219
return $"ID: {id}, Custom Value: {param.Value}";
2320
});
24-
25-
// Endpoint showing validation example
21+
// </snippet_IBindableFromHttpContext>
22+
// <snippet_validation>
2623
app.MapGet("/validated", (ValidatedParameter param) =>
2724
{
2825
if (string.IsNullOrEmpty(param.Value))
@@ -32,6 +29,6 @@
3229

3330
return Results.Ok($"Validated value: {param.Value}");
3431
});
35-
// <//snippet_IBindableFromHttpContext>
32+
// </snippet_validation>
3633

3734
app.Run();

aspnetcore/fundamentals/minimal-apis/includes/responses7-8.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public static void PopulateMetadata(MethodInfo method, EndpointBuilder builder)
229229
builder.Metadata.Add(new ProducesAttribute(MediaTypeNames.Text.Html));
230230
}
231231
```
232-
## Custom parameter binding with IBindableFromHttpContext
232+
## Custom parameter binding with `IBindableFromHttpContext`
233233

234234
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.
235235

@@ -243,7 +243,15 @@ public interface IBindableFromHttpContext<TSelf>
243243

244244
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:
245245

246-
:::code language="csharp" source="~/fundamentals/minimal-apis/8.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
246+
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
247+
248+
The following is an example implementation of a custom parameter that binds from an HTTP header:
249+
250+
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/CustomBoundParameter.cs":::
251+
252+
You can also implement validation within your custom binding logic:
253+
254+
:::code language="csharp" source="~/fundamentals/minimal-apis/8.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
247255

248256
[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))
249257

aspnetcore/fundamentals/minimal-apis/includes/responses9.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ An alternative approach is using the <xref:Microsoft.AspNetCore.Mvc.ProducesAttr
223223

224224
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_11":::
225225

226-
## Custom parameter binding with IBindableFromHttpContext
226+
## Custom parameter binding with `IBindableFromHttpContext`
227227

228228
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.
229229

@@ -237,7 +237,15 @@ public interface IBindableFromHttpContext<TSelf>
237237

238238
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:
239239

240-
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
240+
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
241+
242+
The following is an example implementation of a custom parameter that binds from an HTTP header:
243+
244+
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/CustomBoundParameter.cs":::
245+
246+
You can also implement validation within your custom binding logic:
247+
248+
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
241249

242250
[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))
243251

aspnetcore/fundamentals/minimal-apis/responses.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ An alternative approach is using the <xref:Microsoft.AspNetCore.Mvc.ProducesAttr
274274

275275
:::code language="csharp" source="~/fundamentals/minimal-apis/9.0-samples/Snippets/Program.cs" id="snippet_11":::
276276

277-
## Custom parameter binding with IBindableFromHttpContext
277+
## Custom parameter binding with `IBindableFromHttpContext`
278278

279279
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.
280280

@@ -290,6 +290,14 @@ By implementing the <xref:Microsoft.AspNetCore.Http.IBindableFromHttpContext%601
290290

291291
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/Program.cs" id="snippet_IBindableFromHttpContext":::
292292

293+
The following is an example implementation of a custom parameter that binds from an HTTP header:
294+
295+
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/CustomBoundParameter.cs":::
296+
297+
You can also implement validation within your custom binding logic:
298+
299+
:::code language="csharp" source="~/fundamentals/minimal-apis/10.0-samples/CustomBindingExample/Program.cs" id="snippet_Validation":::
300+
293301
[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))
294302

295303
## Configure JSON serialization options

0 commit comments

Comments
 (0)