当前位置:网站首页>Miniapi of. Net7 (special section): NET7 Preview3
Miniapi of. Net7 (special section): NET7 Preview3
2022-04-23 03:06:00 【Dotnet cross platform】
.NET7 The third preview version of has been released , It also brings mini api Update , This time we bring the routing filter , And mvc Version of action Filter similarity . Specific view https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-dotnet-7-preview-3/
Use the official code , Can't run , Such and such questions , Consider the following code , Directly report the exception of null reference .
app.MapGet("/data0/{no}", (string no) =>
{
Console.WriteLine($"Get In the method :no={no}");
return new Data { No = no, Name = "test" + DateTime.Now };
}).AddFilter((RouteHandlerInvocationContext context, RouteHandlerFilterDelegate next) =>
{
var no = (string?)context.Parameters[0];
Console.WriteLine($"Get Before the method :no={no}");
if (no != null && !no.StartsWith("NO"))
{
return new ValueTask<object?>("no is error!");
}
var result = next(context);
if (result.IsCompleted)
{
Console.WriteLine($"Get After the method : result ={result.Result}");
}
return result;
});
After a while of tossing and turning , Finally, I started running normally , Need to put map The method of writing can be written independently . At the same time, there are three ways to use filters , The soup does not change the dressing , The essence is the same .
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
Data GetData(string no)
{
Console.WriteLine($"Get In the method :no={no}");
return new Data { No = no, Name = "test" + DateTime.Now };
};
// The first way
app.MapGet("/data1/{no}", GetData)
.AddFilter((RouteHandlerInvocationContext context, RouteHandlerFilterDelegate next) =>
{
var no = (string?)context.Parameters[0];
Console.WriteLine($"Get Before the method :no={no}");
if (no != null && !no.StartsWith("NO"))
{
return new ValueTask<object?>("no is error!");
}
var result = next(context);
if (result.IsCompleted)
{
Console.WriteLine($"Get After the method : result ={result.Result}");
}
return result;
});
string AddTest(Data data)
{
Console.WriteLine($"Post In the method :no={data.No}");
return "OK";
}
app.MapPost("/data1", AddTest)
.AddFilter((RouteHandlerInvocationContext context, RouteHandlerFilterDelegate next) =>
{
var data = (Data?)context.Parameters[0];
Console.WriteLine($"Post Before the method :data={data}");
var result = next(context);
if (result.IsCompleted)
{
Console.WriteLine($"Post After the method : result ={result.Result}");
}
return result;
});
// The second way
app.MapGet("/data2/{no}", GetData)
.AddFilter((RouteHandlerContext routeHandlerContext, RouteHandlerFilterDelegate next) =>
{
return (context) =>
{
var no = (string?)context.Parameters[0];
if (no != null && !no.StartsWith("NO"))
{
return new ValueTask<object?>("no is error!");
}
return next(context);
};
});
// The third way
app.MapGet("/data3/{name}", GetData).AddFilter<MyFilter>();
app.Run();
public class MyFilter : IRouteHandlerFilter
{
public ValueTask<object?> InvokeAsync(RouteHandlerInvocationContext context, RouteHandlerFilterDelegate next)
{
var no = (string?)context.Parameters[0];
if (no != null && !no.StartsWith("NO"))
{
return new ValueTask<object?>("no is error!");
}
return next(context);
}
}
public record Data
{
public string No { get; set; }
public string Name { get; set; }
}
版权声明
本文为[Dotnet cross platform]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230301334472.html
边栏推荐
- Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
- Binary tree
- JS relearning
- HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
- ASP.NET 6 中间件系列 - 条件中间件
- 荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
- Gavl021, gavl281, AC220V to 5v200ma small volume non isolated chip scheme
- Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
- HLS / chisel practice CORDIC high performance computing complex square root
- Niuke white moon race 5 [problem solving mathematics field]
猜你喜欢

Detailed log display of openfeign call

Configuring Apache Web services for servers such as Tianyi cloud

ASP.NET和ASP.NETCore多环境配置对比
![[format] simple output (2)](/img/24/64739f5e6bbd54bfa9fb78b8c53c94.png)
[format] simple output (2)

Q-Learning & Sarsa

Source Generator实战

利用栈的回溯来解决“文件的最长绝对路径”问题
![Introduction to ACM [TSP problem]](/img/9f/4e3592542d989b2fbb6d82f7f2fbd2.png)
Introduction to ACM [TSP problem]
![Niuke white moon race 6 [solution]](/img/c5/6c59378c3bb12efa60ab3a8cd2c943.png)
Niuke white moon race 6 [solution]

Introduction and use of openfeign component
随机推荐
Use of MySQL command line client and common commands
Restart redis
TP5 customization in extend directory succeeded and failed. Return information
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
Cherno_ Game engine series tutorial (5): 101~
HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
Wepy learning record
JS relearning
Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
Due to 3 ²+ four ²= five ², Therefore, we call '3,4,5' as the number of Pythagorean shares, and find the array of all Pythagorean shares within n (including n).
荐读 | 分享交易员的书单,向名家请教交易之道,交易精彩无比
TP5 where query one field is not equal to multiple values
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
TP5 email (2020-05-27)
【鉴权/授权】自定义一个身份认证Handler
ASP.NET 6 中间件系列 - 执行顺序
c#可变参数params的介绍
Reverse a linked list < difficulty coefficient >
Deep q-network (dqn)
Two methods are used to solve the "maximum palindrome product" problem