中间件中的HttpContext如何

问题描述 投票:0回答:0

在asp net core(中间件)中我们有:

app.Run(async context => await context.Response.WriteAsync("hello")); 

我有一个问题:如何在asp net core中注入上下文?

我尝试创建方法 MyRun() 并获得相同的结果,但没有成功。


app.MyRun(async context => await context.Response.WriteAsync("hello"));

public static class MyRunExtension
    {

        public static void MyRun(this IApplicationBuilder app, MyRequestDelegate handler)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (handler == null)
            {
                throw new ArgumentNullException(nameof(handler));
            }

            app.Use(handler => handler);
        }

    }

和 ....

    public delegate Task MyRequestDelegate(HttpContext context);
c# dependencies code-injection httpcontext
© www.soinside.com 2019 - 2024. All rights reserved.