当前位置:网站首页>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
边栏推荐
- Encapsulate components such as pull-down menu based on ele
- 先中二叉建树
- Distributed system services
- Encapsulation of ele table
- L2-006 树的遍历(中后序确定二叉树&层序遍历)
- 樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
- Simple example of using redis in PHP
- [format] simple output (2)
- Service avalanche effect
猜你喜欢

Blazor University (12)组件 — 组件生命周期

Configuring Apache Web services for servers such as Tianyi cloud

Depth deterministic strategy gradient (ddpg)

LNMP MySQL allows remote access

Passing object type parameters through openfeign

Recursion - outputs continuously increasing numbers

Blazor University (11)组件 — 替换子组件的属性

Laravel8- use JWT

Ningde's position in the times is not guaranteed?

How to write the expected salary on your resume to double your salary during the interview?
随机推荐
C# 11 对 ref 和 struct 的改进
AC380V drop 5v12v24v200ma, UHV non isolated chip IC scheme
Cherno_ Game engine series tutorial (5): 101~
准备一个月去参加ACM,是一种什么体验?
C# 读写二进制文件
Q-Learning & Sarsa
In redis cluster, the master node fails, and the IP changes after the master-slave switch. The client does not need to deal with it
最通俗易懂的依赖注入之服务容器与作用域
Detailed explanation of distributed things
tf. keras. layers. Density function
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
Gavl021, gavl281, AC220V to 5v200ma small volume non isolated chip scheme
Passing object type parameters through openfeign
[software testing] understand the basic knowledge of software testing
Systemctl start Prometheus + grafana environment
Opencv reads webcam video and saves it locally
eventBus
tf. keras. layers. Timedistributed function
Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
Introduction and use of openfeign component