• 1399/05/06

بهینه کردن استفاده از MiddleWare :

با سلام و خسته نباشید میتونیم از این روش از MiddleWare استفاده کنیم یه مقداری اصولی تر هستش

 

تو یک کلاس جدا :

namespace Service.MiddleWare
{
    public class Test_Middleware
    {
        private readonly RequestDelegate _next;
        public Test_Middleware(RequestDelegate next)
        {         
           _next = next;
        }
        public async Task InvokeAsync(HttpContext context)
        {
            if (...)
                await _next(context);
            else
                await context.Response.WriteAsync("!!!");
        }
    }

    public static class Test_Extension
    {
        public static void UseTestMiddleware(this IApplicationBuilder app)
        {
            app.UseMiddleware<Test_Middleware>();
        }
    }
}

توی Startup : 

app.UseTestMiddleware();
  • 1399/05/06
  • ساعت 17:25

سلام 

بله خیلی خوانا و بهتره 


logo-samandehi