【问题标题】:How to activate middleware如何激活中间件
【发布时间】:2022-01-13 15:04:43
【问题描述】:

我必须在其他地方激活它还是你能帮我?当我运行项目时,它直接进入控制器,中间件没有运行。

这是我的中间件。

public class AuthenticationMiddleWare
{
    private RequestDelegate nextDelegate;
        
    public AuthenticationMiddleWare(RequestDelegate next, IConfiguration config)
    {
        nextDelegate = next;
    }
        
    public async Task Invoke(HttpContext httpContext)
    {
        try
        {
            // somecode
            await nextDelegate.Invoke(httpContext);
        
        }
        catch (Exception ex)
        {
        
        }
    }    
}

【问题讨论】:

    标签: c# asp.net-core asp.net-core-webapi


    【解决方案1】:

    确保您已在 Startup 类的 Configure 方法中注册了您的中间件。

    public class Startup
    {
        ...
    
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseMiddleware<AuthenticationMiddleWare>();
        }
    }
    

    参考

    The Configure method - App startup in ASP.NET Core

    猜你喜欢
    • 2019-11-02
    • 2019-08-26
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2011-01-13
    • 2019-07-13
    • 2012-01-27
    相关资源
    最近更新 更多