publicinterfaceIApplicationBuilder { // // 摘要: // Adds a middleware delegate to the application's request pipeline. // // 参数: // middleware: // The middleware delegate. // // 返回结果: // The Microsoft.AspNetCore.Builder.IApplicationBuilder. IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware); }
上面这种中间用来进行中间件间的转发,里面根据业务情况来决定处理请求发给哪个中间件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicstaticclassUseExtensions { // // 摘要: // Adds a middleware delegate defined in-line to the application's request pipeline. // // 参数: // app: // The Microsoft.AspNetCore.Builder.IApplicationBuilder instance. // // middleware: // A function that handles the request or calls the given next function. // // 返回结果: // The Microsoft.AspNetCore.Builder.IApplicationBuilder instance. publicstatic IApplicationBuilder Use(this IApplicationBuilder app, Func<HttpContext, Func<Task>, Task> middleware); }
作为终端的中间件
1 2 3 4 5 6 7
publicvoidConfigure(IApplicationBuilder app, IWebHostEnvironment env) { app.Run(async context => { await context.Response.WriteAsync("This is a middleware"); }); }