闽公网安备 35020302035485号
4.日志记录配置
using System.Text.Json.Serialization;
using Demo03;
// 堆代码 duidaima.com
var builder = WebApplication.CreateSlimBuilder(args);
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});
var app = builder.Build();
var sampleTodos = TodoGenerator.GenerateTodos().ToArray();
var todosApi = app.MapGroup("/todos");
todosApi.MapGet("/", () => sampleTodos);
todosApi.MapGet("/{id}", (int id) =>
sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo
? Results.Ok(todo)
: Results.NotFound());
app.Run();
[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
通过给一个类AppJsonSerializerContext来添加特性[JsonSerializable(typeof(Todo[]))],来替换掉在对象和JSON之间的转换。同时,AOT类型的项目也不能基于IIS来调试,所以对launchSettings.json作了调整。下面是官方给出的功能支持力度,相当一部分功能已实现。但一些三方的功能也是需要时间对应的,好饭不怕晚,让我们静候AOT的到来吧。
| 功能 | 完全支持 | 部分支持 | 不支持 |
|---|---|---|---|
| gRPC | ✔️完全支持 |
|
|
| 最小 API |
|
✔️部分支持 |
|
| MVC |
|
|
❌❌ |
| Blazor Server |
|
|
❌❌ |
| SignalR |
|
|
❌❌ |
| Authentication |
|
|
❌(即将推出 JWT) |
| CORS | ✔️完全支持 |
|
|
| HealthChecks | ✔️完全支持 |
|
|
| HttpLogging | ✔️完全支持 |
|
|
| 本地化 | ✔️完全支持 |
|
|
| OutputCaching | ✔️完全支持 |
|
|
| RateLimiting | ✔️完全支持 |
|
|
| RequestDecompression | ✔️完全支持 |
|
|
| ResponseCaching | ✔️完全支持 |
|
|
| ResponseCompression | ✔️完全支持 |
|
|
| Rewrite | ✔️完全支持 |
|
|
|
Session |
|
|
❌❌ |
| Spa |
|
|
❌❌ |
| StaticFiles | ✔️完全支持 |
|
|
| WebSockets | ✔️完全支持 |
|
|