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();
});
}

.Net Core 创建和使用中间件的更多相关文章

  1. 在ASP.NET Core 中使用Cookie中间件

    在ASP.NET Core 中使用Cookie中间件 ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分 ...

  2. asp.net core mvc 管道之中间件

    asp.net core mvc 管道之中间件 http请求处理管道通过注册中间件来实现各种功能,松耦合并且很灵活 此文简单介绍asp.net core mvc中间件的注册以及运行过程 通过理解中间件 ...

  3. 在ASP.NET Core 中使用Cookie中间件 (.net core 1.x适用)

    在ASP.NET Core 中使用Cookie中间件 ASP.NET Core 提供了Cookie中间件来序列化用户主题到一个加密的Cookie中并且在后来的请求中校验这个Cookie,再现用户并且分 ...

  4. ASP.NET CORE 管道模型及中间件使用解读

    说到ASP.NET CORE 管道模型不得不先来看看之前的ASP.NET 的管道模型,两者差异很大,.NET CORE 3.1 后完全重新设计了框架的底层,.net core 3.1 的管道模型更加灵 ...

  5. NET Core 拓展方法和中间件集合(支持NET Core2.0+)

    # Pure.NETCoreExtentensions https://github.com/purestackorg/Pure.NETCoreExtensions NET Core 拓展方法和中间件 ...

  6. 使用.NET Core创建Windows服务(二) - 使用Topshelf方式

    原文:Creating Windows Services In .NET Core – Part 2 – The "Topshelf" Way 作者:Dotnet Core Tut ...

  7. 使用.NET Core创建Windows服务 - 使用.NET Core工作器方式

    原文:Creating Windows Services In .NET Core – Part 3 – The ".NET Core Worker" Way 作者:Dotnet ...

  8. .NET Core 创建Windows服务

    .NET Core 创建Windows服务 作者:高堂 原文地址:https://www.cnblogs.com/gaotang/p/10850564.html 写在前面 使用 TopShelf+Au ...

  9. 使用.NET Core创建Windows服务(一) - 使用官方推荐方式

    原文:使用.NET Core创建Windows服务(一) - 使用官方推荐方式 原文:Creating Windows Services In .NET Core – Part 1 – The &qu ...

随机推荐

  1. iOS UITableView动态隐藏或显示Item

    通过改变要隐藏的item的高度实现隐藏和显示item 1.创建UITableView #import "ViewController.h" @interface ViewContr ...

  2. 喜迎Win10周年版,芒果TV UWP完善手机视频离线下载

    喜迎Win10周年版更新,湖南卫视旗下唯一官方视频平台<芒果TV>近日向Win10商店提交了芒果TV UWP V3.1.0版,这次不仅在上一版(V3.0.0)的基础上完善了用户呼声最高的手 ...

  3. 通过NLayer和NAudio转换MP3成WAV

    NuGet安装: Install-Package NLayer.NAudioSupport 示例代码: using Microsoft.Win32; using NAudio.Wave; using ...

  4. Unicode 7.0.1中文支持非常好

    简单测试了一下,7.0.1中文支持非常好.Delphi7下将UniConnection的useUnicode设置为False,Tokyo下设置为True,Charset空着即可. 问题要点:1.建数据 ...

  5. Ural_1169_Pairs

    此题略坑... 思路:把N个点分成m若干个联通子图,然后用m-1个桥把这m个联通子图连接起来即可. 若每个联通子图内部都是完全图也符合题意,但答案却是Wrong Answer,只有把每个联通子图内部当 ...

  6. 获取原生DOM,diy脚手架,vue-clide使用,element-ui的使用

    一.获取原生DOM的方式 给标签或者属性添加ref属性 //1.添加属性 <div ref='shy'><div> <Home ref='home'></Ho ...

  7. serverless 项目配置及创建helloworld应用(二)

    阅读目录 一:学习使用AWS Lambda来作为服务器引擎 二:使用serverless环境搭建 三:创建我们的第一个应用,hello world 服务 回到顶部 一:学习使用AWS Lambda来作 ...

  8. 3014C语言_运算符

    第四章 运算符 4.1 分类 C语言的运算符范围很广,可分为以下几类: 1.算术运算符:用于各类数值运算.包括加(+).减(-).乘(*).除(/).求余(%).自增(++).自减(--)共七种. 2 ...

  9. 【转】php7对redis的扩展及redis主从搭建

    一:redis安装     1:下载并安装 cd /home/software wget http://download.redis.io/releases/redis-3.2.3.tar.gz ta ...

  10. uint16,uint32是什么?

    记得之前在刷笔试题的时候就看见过这个问题,发现当时上网百度后又忘了. 最近在看CryEngine3引擎代码的时候又晕了,趁现在赶紧记下来~ 在查看CE3的代码时我发现了这个变量,TFlowNodeId ...