当前位置:网站首页>.NET7之MiniAPI(特别篇):.NET7 Preview3
.NET7之MiniAPI(特别篇):.NET7 Preview3
2022-04-23 03:02:00 【dotNET跨平台】
.NET7的第三个预览版发布了,同样带来了mini api的更新,这次带来了路由过滤器,与mvc版的action 过滤器相似。具体见https://devblogs.microsoft.com/dotnet/asp-net-core-updates-in-dotnet-7-preview-3/
用官方的代码,跑不起来,这样那样的问题,比如下面的代码,直接报空引用的异常。
app.MapGet("/data0/{no}", (string no) =>
{
Console.WriteLine($"Get方法中: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方法前: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方法后:结果={result.Result}");
}
return result;
});
经过一阵折腾,终于正常的跑了起来,需要把map的方法独立出来写才可以。同时这里使用了三种方式来使用过滤器,换汤不换药,本质一样。
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
Data GetData(string no)
{
Console.WriteLine($"Get方法中:no={no}");
return new Data { No = no, Name = "test" + DateTime.Now };
};
//第一种方式
app.MapGet("/data1/{no}", GetData)
.AddFilter((RouteHandlerInvocationContext context, RouteHandlerFilterDelegate next) =>
{
var no = (string?)context.Parameters[0];
Console.WriteLine($"Get方法前: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方法后:结果={result.Result}");
}
return result;
});
string AddTest(Data data)
{
Console.WriteLine($"Post方法中:no={data.No}");
return "OK";
}
app.MapPost("/data1", AddTest)
.AddFilter((RouteHandlerInvocationContext context, RouteHandlerFilterDelegate next) =>
{
var data = (Data?)context.Parameters[0];
Console.WriteLine($"Post方法前:data={data}");
var result = next(context);
if (result.IsCompleted)
{
Console.WriteLine($"Post方法后:结果={result.Result}");
}
return result;
});
//第二种方式
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);
};
});
//第三种方式
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跨平台]所创,转载请带上原文链接,感谢
https://blog.csdn.net/sd7o95o/article/details/124239607
边栏推荐
- Opencv combines multiple pictures into video
- Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
- Introduction to ACM [TSP problem]
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
- tf. keras. layers. Inputlayer function
- Kubernetes - detailed explanation of pod
- Huawei machine test question -- deformation of hj53 Yang Hui triangle
- Basic workflow of CPU
- Face longitude:
- Openfeign details show
猜你喜欢
L2-006 树的遍历(中后序确定二叉树&层序遍历)
Detailed log display of openfeign call
Log cutting - build a remote log collection server
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
BLDC double closed loop (speed PI + current PI) Simulink simulation model
Airtrack cracking wireless network password (Dictionary running method)
The input of El input input box is invalid, and error in data(): "referenceerror: El is not defined“
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
AspNetCore配置多环境log4net配置文件
Response processing of openfeign
随机推荐
Dynamic sequence table + OJ
《信息系统项目管理师总结》第六章 项目人力资源管理
Introduction to ACM [inclusion exclusion theorem]
Detailed explanation of distributed things
Xamarin效果第二十二篇之录音效果
Close the computer port
Restart redis
[Euler plan question 13] sum of large numbers
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
SQL statement - DDL
tf. keras. layers. Inputlayer function
How to use C language to realize [guessing numbers game]
最通俗易懂的依赖注入之服务容器与作用域
C#语法糖空合并运算符【??】和空合并赋值运算符【 ??=】
Chapter V project quality management of information system project manager summary
Opencv combines multiple pictures into video
[learn junit5 from official documents] [II] [writingtests] [learning notes]
樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統
Linux redis - redis database caching service
Opencv reads webcam video and saves it locally