1. 程式人生 > 資料庫 >第十一章 MySQL日誌詳解

第十一章 MySQL日誌詳解

核心物件

  • IApplicationBuilder
  • RequestDelegate

程式碼演示

            app.Use(async (context, next) =>
            {
                await context.Response.WriteAsync("hello Word");
            });

指定路徑

程式碼演示

            app.MapWhen(context =>
            {
                return context.Request.Query.Keys.Contains("
id"); }, builder => { builder.Use(async (context, next) => { await context.Response.WriteAsync("hello Word"); }); });

自定義中介軟體

中介軟體程式碼

    public class MyMiddleware
    {
        RequestDelegate _next;

        
public MyMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext context) { Console.WriteLine("===========執行前邏輯=========="); await _next(context); Console.WriteLine("===========執行後邏輯=========="); } }

擴充套件方法

    public static class MyBuilderExtensions
    {
        public static IApplicationBuilder UserMyMiddleware(this IApplicationBuilder app)
        {
            return app.UseMiddleware<MyMiddleware>();
        }
    }

Startup類註冊中介軟體

            app.UserMyMiddleware();

異常處理中介軟體

程式碼演示

            app.UseExceptionHandler(errorApp =>
            {
                errorApp.Run(async context =>
                {
                    IExceptionHandlerPathFeature exceptionHandlerPathFeature = context.Features.Get<IExceptionHandlerPathFeature>();
                    context.Response.ContentType = "application/json;charset=utf-8;";
                    context.Response.StatusCode = StatusCodes.Status200OK;
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(exceptionHandlerPathFeature));
                });
            });