1. 定义中间内容

1.1 必须有一个RequestDelegate 委托用了进入一个中间件

1.2 通过构造函数设置这个RequestDelegate委托

1.3 必须有一个方法Task Invoke,在这个方法里编写中间件内容最后执行RequestDelegate委托

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>
/// 参数
/// </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. 编写一个扩展方法用来添加到程序中

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.  在Startup添加中间件

// 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();
}
//扩展方式添加
app.UserTestMiddleWare("this is test param");
//直接添加中间件方式
app.Use((context, next) =>
{
context.Response.WriteAsync("this is test");
return next();
});
}

最新文章

  1. 开窗函数 First_Value 和 Last_Value
  2. ACM 兰州烧饼
  3. RTO &amp; RPO
  4. 九 spring和mybatis整合
  5. RFS一些基本概念
  6. 如果解决ubuntu tab键不能提示命令
  7. Javascript的四种继承方式
  8. flex中DataGrid里使用itemRenderer后数据无法绑定到数据源的问题
  9. oracle 11g ora-01843 无效月份
  10. OpenCV学习笔记:矩阵的掩码操作
  11. 【设计模式 - 2】之单例模式(Singleton)
  12. UVALIVE 5893 计算几何+搜索
  13. [每日一题] OCP1z0-047 :2013-07-25 权限――角色与对象权限
  14. 去空格 whitespaceAndNewlineCharacterSet
  15. BZOJ 2795: [Poi2012]A Horrible Poem( hash )
  16. Thrift总结(二)创建RPC服务
  17. Event Sourcing pattern
  18. Java线程基础(二)
  19. 常用算法和Demo(Java实现)(持续更新)
  20. Codeforces 359E Neatness

热门文章

  1. Z-Order
  2. QuickReport根据每行的内容长度动态调整DetailBand1的行高
  3. grep专题
  4. IT回忆录-1
  5. webmethod基本认知
  6. Android零基础入门第83节:Activity间数据传递方法汇总
  7. fprintf函数将格式打印到文件,非常好用(怎么没早点发现这个函数)
  8. How to create my own self signed certificate chain?
  9. 关于联合体union的详细解释
  10. Qt Quick 事件处理之信号与槽(foruok的博客)