show line numbers in conde snippets

This commit is contained in:
Marcello 2023-10-20 18:22:46 +02:00
parent cd1df0e376
commit 255a68d673
82 changed files with 1249 additions and 1251 deletions

View file

@ -2,7 +2,7 @@
> **Note**: Requires .NET 6+
```cs
```cs linenums="1"
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddSingleton<IService, Service>();
@ -18,7 +18,6 @@ app.Run();
app.RunAsync();
```
## Application Settings
App settings are loaded (in order) from:
@ -42,7 +41,7 @@ Setting a value is done with `dotnet user-secrets set <key> <value>`, keys can b
## Swagger
```cs
```cs linenums="1"
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
@ -60,7 +59,7 @@ app.MapPost("/route", Handler).Accepts<Type>(contentType);
## MVC
```cs
```cs linenums="1"
builder.Services.AddControllersWithViews();
//or
builder.Services.AddControllers();
@ -96,7 +95,7 @@ app.MapControllerRoute(
To define routes and handlers using Minimal APIs, use the `Map(Get|Post|Put|Delete)` methods.
```cs
```cs linenums="1"
// the dependencies are passed as parameters in the handler delegate
app.MapGet("/route/{id}", (IService service, int id) => {
@ -113,7 +112,7 @@ IResult Search(int id, int? page = 1, int? pageSize = 10) { /* ... */ }
The `MapGroup()` extension method, which helps organize groups of endpoints with a common prefix.
It allows for customizing entire groups of endpoints with a singe call to methods like `RequireAuthorization()` and `WithMetadata()`.
```cs
```cs linenums="1"
var group = app.MapGroup("<route-prefix>");
group.MapGet("/", GetAllTodos); // route: /<route-prefix>
@ -127,14 +126,14 @@ group.MapGet("/{id}", GetTodo); // route: /<route-prefix>/{id}
The `Microsoft.AspNetCore.Http.TypedResults` static class is the “typed” equivalent of the existing `Microsoft.AspNetCore.Http.Results` class.
It's possible to use `TypedResults` in minimal APIs to create instances of the in-framework `IResult`-implementing types and preserve the concrete type information.
```cs
```cs linenums="1"
public static async Task<IResult> GetAllTodos(TodoDb db)
{
return TypedResults.Ok(await db.Todos.ToArrayAsync());
}
```
```cs
```cs linenums="1"
[Fact]
public async Task GetAllTodos_ReturnsOkOfObjectResult()
{
@ -153,7 +152,7 @@ public async Task GetAllTodos_ReturnsOkOfObjectResult()
The `Results<TResult1, TResult2, TResultN>` generic union types, along with the `TypesResults` class, can be used to declare that a route handler returns multiple `IResult`-implementing concrete types.
```cs
```cs linenums="1"
// Declare that the lambda returns multiple IResult types
app.MapGet("/todos/{id}", async Results<Ok<Todo>, NotFound> (int id, TodoDb db)
{
@ -165,7 +164,7 @@ app.MapGet("/todos/{id}", async Results<Ok<Todo>, NotFound> (int id, TodoDb db)
## Filters
```cs
```cs linenums="1"
public class ExampleFilter : IRouteHandlerFilter
{
public async ValueTask<object?> InvokeAsync(RouteHandlerInvocationContext context, RouteHandlerFilterDelegate next)
@ -178,7 +177,7 @@ public class ExampleFilter : IRouteHandlerFilter
}
```
```cs
```cs linenums="1"
app.MapPost("/route", Handler).AddFilter<ExampleFilter>();
```
@ -192,7 +191,7 @@ With Minimal APIs it's possible to access the contextual information by passing
- `ClaimsPrincipal`
- `CancellationToken` (RequestAborted)
```cs
```cs linenums="1"
app.MapGet("/hello", (ClaimsPrincipal user) => {
return "Hello " + user.FindFirstValue("sub");
});
@ -202,7 +201,7 @@ app.MapGet("/hello", (ClaimsPrincipal user) => {
The `Microsoft.AspNetCore.OpenApi` package exposes a `WithOpenApi` extension method that generates an `OpenApiOperation` derived from a given endpoints route handler and metadata.
```cs
```cs linenums="1"
app.MapGet("/todos/{id}", (int id) => ...)
.WithOpenApi();
@ -218,7 +217,7 @@ app.MapGet("/todos/{id}", (int id) => ...)
Using [Minimal Validation](https://github.com/DamianEdwards/MinimalValidation) by Damian Edwards.
Alternatively it's possible to use [Fluent Validation](https://fluentvalidation.net/).
```cs
```cs linenums="1"
app.MapPost("/widgets", (Widget widget) => {
var isValid = MinimalValidation.TryValidate(widget, out var errors);
@ -241,7 +240,7 @@ class Widget
## JSON Serialization
```cs
```cs linenums="1"
// Microsoft.AspNetCore.Http.Json.JsonOptions
builder.Services.Configure<JsonOptions>(opt =>
{
@ -251,7 +250,7 @@ builder.Services.Configure<JsonOptions>(opt =>
## Authorization
```cs
```cs linenums="1"
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer();
builder.Services.AddAuthorization();
@ -284,13 +283,13 @@ app.MapGet("/special-secret", () => "This is a special secret!")
The `user-jwts` tool is similar in concept to the existing `user-secrets` tools, in that it can be used to manage values for the app that are only valid for the current user (the developer) on the current machine.
In fact, the `user-jwts` tool utilizes the `user-secrets` infrastructure to manage the key that the JWTs will be signed with, ensuring its stored safely in the user profile.
```sh
```sh linenums="1"
dotnet user-jwts create # configure a dev JWT fot the current user
```
## Output Caching
```cs
```cs linenums="1"
builder.Services.AddOutputCaching(); // no special options
builder.Services.AddOutputCaching(options =>
{
@ -322,7 +321,7 @@ app.MapGet("/<route>", [OutputCache(/* options */)]RouteHandler);
### Cache Eviction
```cs
```cs linenums="1"
app.MapGet("/<route-one>", RouteHandler).CacheOutput(x => x.Tag("<tag>")); // tag cache portion
@ -334,11 +333,11 @@ app.MapGet("/<route-two>", (IOutputCacheStore cache, CancellationToken token) =>
### Custom Cache Policy
```cs
```cs linenums="1"
app.MapGet("/<route-one>", RouteHandler).CacheOutput(x => x.AddCachePolicy<CustomCachePolicy>());
```
```cs
```cs linenums="1"
class CustomCachePolicy : IOutputCachePolicy
{
public ValueTask CacheRequestAsync(OutputCacheContext context, CancellationToken cancellationToken) { }
@ -353,7 +352,7 @@ class CustomCachePolicy : IOutputCachePolicy
The *options pattern* uses classes to provide strongly-typed access to groups of related settings.
```json
```json linenums="1"
{
"SecretKey": "Secret key value",
"TransientFaultHandlingOptions": {
@ -370,7 +369,7 @@ The *options pattern* uses classes to provide strongly-typed access to groups of
}
```
```cs
```cs linenums="1"
// options model for binding
public class TransientFaultHandlingOptions
{
@ -379,13 +378,13 @@ public class TransientFaultHandlingOptions
}
```
```cs
```cs linenums="1"
// setup the options
builder.Services.Configure<TransientFaultHandlingOptions>(builder.Configuration.GetSection<TransientFaultHandlingOptions>(nameof(Options)));
builder.Services.Configure<TransientFaultHandlingOptions>(builder.Configuration.GetSection<TransientFaultHandlingOptions>(key));
```
```cs
```cs linenums="1"
class DependsOnOptions
{
private readonly IOptions<TransientFaultHandlingOptions> _options;