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. linux系统的ssh服务开启方法

      操作方法: 1.编辑sshd_config文件 root@linux:~# vi /etc/ssh/sshd_config 2.检查如下配置项: PasswordAuthentication ye ...

  2. DataGridView 中发生以下异常: System.Exception: 是 不是 Decimal 的有效值。 ---> System.FormatException: 输入字符串的格式不正确。

    其实之前我自己是没测出这个问题的,但是一放到测试的手上就出来了,原因我知道在哪里改输什么东西,但是人家不知道啊.报错如下: --------------------------- “DataGridV ...

  3. js中的scrollTop、offsetTop、clientTop

    scrollHeight:获取对象可滚动的高度. scrollWidth:获取对象可滚动的宽度. scrollTop:获取对象最顶端与对象可见区域最顶端的距离. scrollLeft:获取对象左边界与 ...

  4. Ubuntu服务器搭建

    Ubuntu16 搭建Git 服务器 - 濮成林 - 博客园 https://www.cnblogs.com/charliePU/p/7528226.html Ubuntu 搭建 GitLab 笔记 ...

  5. Android零基础入门第8节:HelloWorld,我的第一趟旅程出发点

    原文:Android零基础入门第8节:HelloWorld,我的第一趟旅程出发点 经过前面几期的学习,我们知道了Android的前世今生,也大致了解了Android的系统架构和应用组件,然后花了几期来 ...

  6. winform picturebox设置布局样式

    这里分背景图和直接显示的图片的布局 背景图的布局为BackgroundImageLayout设置为strecth即为自动拉伸 图片的布局为SizeMode 设置为StretchImage即自动拉伸

  7. .NET Core整合log4net以及全局异常捕获实现2

    Startup代码 public static ILoggerRepository repository { get; set; } public Startup(IConfiguration con ...

  8. Qt4编译生成VS静态库(静态编译),有三个bat文件 good

    开发环境:vs2008+Qt4.8.4源码库 其他环境请自己尝试,原理应该是差不多的 Qt编译生成静态库 1.         本教程只针对在win32平台,使用VS开发工具(例子以VS2008为例) ...

  9. QString unsigned char* 的转换

    QString -> unsigned char* : QString str = "ABCD";  int length = str.length(); unsigned ...

  10. 抓取报表ALV GRID上的数据

    在项目开发过程中需要从标准报表MB5B中获取数据,以下是本人实例中的相关部分,程序同样适用于获取其他标准报表的数据. CL_SALV_BS_RUNTIME_INFO=>SET(    DISPL ...