当前位置:网站首页>.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
边栏推荐
- REINFORCE
- 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
- VirtualBox virtual machine (Oracle VM)
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (10)
- Chapter V project quality management of information system project manager summary
- 基于多态的职工管理系统源码与一些理解
- Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
- eventBus
- tf. keras. layers. MaxPooling? D function
- Planning code ROS migration POMDP prediction planning (I)
猜你喜欢

BLDC double closed loop (speed PI + current PI) Simulink simulation model

Summary of software test interview questions

The way to conquer C language

微软是如何解决 PC 端程序多开问题的——内部实现

C# 11 的这个新特性,我愿称之最强!

AspNetCore配置多环境log4net配置文件

Linux redis - redis database caching service

樹莓派開發筆記(十二):入手研華ADVANTECH工控樹莓派UNO-220套件(一):介紹和運行系統

First knowledge of C language ~ branch statements

Detailed log display of openfeign call
随机推荐
B blocks of the 46th ICPC Asian regional competition (Kunming)
JS learning notes
腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
First knowledge of C language ~ branch statements
Detailed log display of openfeign call
Basic workflow of CPU
Table space capacity query and expansion of Oracle Database
Response processing of openfeign
Restart redis
Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
Leangoo brain map - shared multi person collaborative mind mapping tool
[software testing] understand the basic knowledge of software testing
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
tf. keras. layers. Density function
Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
Reverse a linked list < difficulty coefficient >
Binary tree
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (9)
Dynamic sequence table + OJ
[if you want to do a good job, you must first use its tools] Guide for downloading and using paper editing and document management (endnote, latex, jabref, overflow) resources