.NET Core: Middleware

  Middleware is assembled to the conduit application software to handle requests and responses, more bonded functional system using middleware.
  Each component:
  selecting whether to pass the request to the next pipeline component.
  You can call before the next component in the pipeline and after implementation.
  Delegates the request (Request delegates) for constructing the request pipeline, each HTTP request processing. Delegate requests use Run, Map and configured Use extension methods. Separate request can delegate inline anonymous method (referred to as inline middleware) is specified, or it may be defined in the reusable class. These reusable classes and anonymous method is inline middleware, or a middleware component. Each middleware component requests processes are responsible for calling the next component in the pipeline, if appropriate, is responsible for linking short-circuit. HTTP module to migrate the middleware explains the differences between the previous version and ASP.NET Core (ASP.NET) request in the pipeline.

(1)Run
public void Configure(IApplicationBuilder app)
{
  app.Run(async context =>
  {
    await context.Response.WriteAsync("Hello, World!");
  });
}

(2)Map
public void Configure(IApplicationBuilder app)
{
  app.Map("/map1", HandleMapTest1);
}

(3)Use
  在 Startup.Configure 方法中启用中间件,例如 app.UseExceptionHandling();。
public static class ExceptionHandlingExtensions
{
  public static IApplicationBuilder UseExceptionHandling( this IApplicationBuilder builder)
  {
    return builder.UseMiddleware<ExceptionHandlingMiddleware>();
  }
}

public class ExceptionHandlingMiddleware
{
  private readonly Next;
  public ExceptionHandlingMiddleware (RequestDelegate next)
  {
    Next= next;
  }
  public async Task Invoke(HttpContext context)
  {
    await Next(context);
  }
}

  Middleware can use custom exception handling mechanism.

Guess you like

Origin www.cnblogs.com/liusuqi/p/11883311.html