gpt4 book ai didi

c# - 如何在 ASP.NET Core 中使用 IApplicationBuilder 中间件重载

转载 作者:太空狗 更新时间:2023-10-30 00:38:16 29 4
gpt4 key购买 nike

ASP.NET Core 为 app.Use() 方法提供了两个重载。通常我们只使用一个重载,即

app.Use(Func<HttpContext,Func<Task>, Task> middleware)

用作

app.Use(async (context, next) => {
await context.Response.WriteAsync("1st middleware <br/>");
await next.Invoke();
});

我想使用的另一个重载是

app.Use(Func<RequestDelegate,RequestDelegate> middleware)

我找不到如何使用此重载的示例。任何想法都会很棒。

最佳答案

Func<RequestDelegate, RequestDelegate>是一个委托(delegate),它接受一个委托(delegate)并返回一个委托(delegate)。您可以将它与此 lambda 表达式一起使用:

app.Use(next => async context => 
{
await context.Response.WriteAsync("Hello, World!");
await next(context);
}

关于c# - 如何在 ASP.NET Core 中使用 IApplicationBuilder 中间件重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42898530/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com