当前位置:网站首页>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
边栏推荐
- What kind of experience is it to prepare for a month to participate in ACM?
- L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
- 求二叉树的叶子结点个数
- Restart redis
- AC380V drop 5v12v24v200ma, UHV non isolated chip IC scheme
- 使用DFS来解决“字典序排数”问题
- Opencv reads webcam video and saves it locally
- Source Generator实战
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
- Detailed log display of openfeign call
猜你喜欢
Blazor University (12)组件 — 组件生命周期
Realize QQ login with PHP
基于.NetCore开发博客项目 StarBlog - (1) 为什么需要自己写一个博客?
Binary tree
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
TP5 email (2020-05-27)
Niuke white moon race 6 [solution]
由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
最通俗易懂的依赖注入之服务容器与作用域
The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
随机推荐
Creating wechat voucher process with PHP
Chapter VII project communication management of information system project manager summary
SQL statement - DDL
JS learning notes
ASP.NET 6 中间件系列 - 条件中间件
Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
Small companies don't make formal offers
The difference between encodeuri and encodeuricomponent
tf. keras. layers. Inputlayer function
C#中切片语法糖的使用
BLDC double closed loop (speed PI + current PI) Simulink simulation model
Laravel new route file
Sonic cloud real machine tutorial
ASP.NET和ASP.NETCore多环境配置对比
Q-Learning & Sarsa
Restart redis
微软是如何解决 PC 端程序多开问题的
tf. keras. layers. Embedding function
Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
Depth deterministic strategy gradient (ddpg)