当前位置:网站首页>.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
边栏推荐
- Niuke white moon race 6 [solution]
- Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
- Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
- The input of El input input box is invalid, and error in data(): "referenceerror: El is not defined“
- Detailed log display of openfeign call
- C# WPF UI框架MahApps切换主题
- ele之Table表格的封装
- What is the difference between varchar and char?
- Redis Cluster集群,主节点故障,主从切换后ip变化,客户端需要处理不
- Difference between relative path and absolute path (often asked in interview)
猜你喜欢

tf. keras. layers. Conv? D function

Leangoo brain map - shared multi person collaborative mind mapping tool

Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)

Cherno_ Game engine series tutorial (5): 101~

Huawei machine test question -- deformation of hj53 Yang Hui triangle

JS learning notes

C# WPF UI框架MahApps切换主题

Error installing Mongo service 'mongodb server' on win10 failed to start

Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
![[software testing] understand the basic knowledge of software testing](/img/ff/8fcd4b88de28505989aaf517d16113.png)
[software testing] understand the basic knowledge of software testing
随机推荐
The space between the left and right of the movie ticket seats is empty and cannot be selected
Distributed system services
Summary of software test interview questions
Regular object type conversion tool - Common DOM class
Passing object type parameters through openfeign
Dynamic sequence table + OJ
c#可变参数params的介绍
[format] simple output (2)
Centos7 install MySQL 8 0
Niuke white moon race 6 [solution]
JS learning notes
Onenet connection process
FileNotFoundError: [Errno 2] No such file or directory
Chapter IV project cost management of information system project manager summary
REINFORCE
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
Codeforces round 784 (Div. 4) (a - H)
Openfeign details show
Specific field information of MySQL export table (detailed operation of Navicat client)