1. Define intermediate content

1.1 There must be oneRequestDelegate Delegate to enter a middleware

1.2 Set this through the constructorRequestDelegate Entrust

1.3 There must be a wayTask Invoke, In this method, the middleware content is written and finally executedRequestDelegate Entrust
using Microsoft.AspNetCore.Http; using System; using
System.Collections.Generic;using System.Linq; using System.Threading.Tasks;
namespace Haos.Develop.CoreTest { public class TestMiddleware { protected
RequestDelegate Next;/// <summary> /// parameter /// </summary> public string Str { get
;set; } public TestMiddleware(RequestDelegate next,string s) { Next = next; Str
= s; } public virtual Task Invoke(HttpContext context) {
context.Response.WriteAsync("this is test string"); return Next(context); } } }
2. Write an extension method to add to the program
using Haos.Develop.CoreTest.Service; using Microsoft.AspNetCore.Builder; using
Microsoft.Extensions.DependencyInjection;using System; using
System.Collections.Generic;using System.Linq; using System.Threading.Tasks;
namespace Haos.Develop.CoreTest { public static class Extension { public static
IApplicationBuilder UserTestMiddleWare(this IApplicationBuilder app, string
str) {return app.UseMiddleware<TestMiddleware>(str); } } }
3.   stayStartup Add Middleware
// This method gets called by the runtime. Use this method to configure the
HTTP request pipeline. public void Configure(IApplicationBuilder app,
IHostingEnvironment env) {if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage(); }// Extension mode add app.UserTestMiddleWare("this is test
param"); // Add middleware directly app.Use((context, next) => { context.Response.WriteAsync("
this is test"); return next(); }); }