How to use IApplicationBuilder middleware overload in ASP.NET Core

偶尔善良 提交于 2020-01-11 09:16:29

问题


ASP.NET Core provides two overloads for app.Use() method. Usually we use only one overload that is

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

Which is used as

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

The other overload that i want to use is

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

I couldn't find an example of how we can use this overload. Any ideas will be great.


回答1:


Func<RequestDelegate, RequestDelegate> is a delegate, which accepts a delegate and returns a delegate. You can use it with this lambda expression:

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


来源:https://stackoverflow.com/questions/42898530/how-to-use-iapplicationbuilder-middleware-overload-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!