jsp 哪些网站,利用技术搭建网站做网站代理,网上接活的平台有哪些,专门下载工程建设标准的网站什么是中间件 中间件是在管道中处理Request请求与Responses响应的一种组件#xff0c;每种组件可以选择是否让Request进入到下一个组件去处理。 译得不好#xff0c;大家可以自己看原文Middleware 更详细的还可以参照园中大神的作品#xff1b; 有汤姆大叔的解读ASP.NET 5 每种组件可以选择是否让Request进入到下一个组件去处理。 译得不好大家可以自己看原文Middleware 更详细的还可以参照园中大神的作品 有汤姆大叔的解读ASP.NET 5 MVC6系列6Middleware详解 artech大神的 ASP.NET Core真实管道详解[1]中间件是个什么东西 怎么创建一个Middleware请参考英文文档Middleware 或者 LineZero的 ASP.NET Core 开发-中间件(Middleware) 要正确使用Middleware来构建自己的应该程序需要理解Run,Use,Map,MapThen这四个方法是如何使用的 下面Ricman将自己的理解与大家分享。 一、Run扩展方法 Run方法在说明上是这样的:在管道的尾端增加一个Middleware它是执行的最后一个Middleware。即它执行完就不再执行下一个Middleware了。如下代码示例。 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection(Logging));loggerFactory.AddDebug(); var loger loggerFactory.CreateLogger(TestLogger); //第一个Run 执行了app.Run(async context {loger.LogInformation(run 1 start); await context.Response.WriteAsync(hello world!,run 1);loger.LogInformation(run 1 end);}); //第二个Run 没的执行app.Run(async context {loger.LogInformation(run 2 start); await context.Response.WriteAsync(hello world!,run 2);loger.LogInformation(run 2 end);});} 输出的结果为 只打印出了第一个Run中的内容。而程序也不会响应第二个Run方法中的内容。 二、Use扩展方法 Use方法则是在管道中增加一个Middleware。如果调用了next.Invoke()方法它会去执行下一个Middleware 。我们把上面的例子稍作修改 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection(Logging));loggerFactory.AddDebug(); var loger loggerFactory.CreateLogger(TestLogger); //use 方法 执行了app.Use (async (context,next) {loger.LogInformation(Use 1 start); await context.Response.WriteAsync(hello world!,Use 1);loger.LogInformation(Use 1 end);}); //Run 方法没的执行app.Run(async context {loger.LogInformation(run 1 start); await context.Response.WriteAsync(hello world!,run 1);loger.LogInformation(run 1 end);});} 输出结果是什么 没有调用next.Invoke();尾端的Middleware即Run方法内没有执行。使用Use方法而没有调用next.Invoke()Use的效果与Run的效果是一致的。为了验证Use 的效果我们再修改代码。 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection(Logging));loggerFactory.AddDebug(); var loger loggerFactory.CreateLogger(TestLogger); //执行了app.Use (async (context,next) {loger.LogInformation(Use 1 start); await context.Response.WriteAsync(hello world!,Use ); await next.Invoke();loger.LogInformation(Use 1 end);}); //没的执行app.Run(async context {loger.LogInformation(run 1 start); await context.Response.WriteAsync( hello world!,run );loger.LogInformation(run 1 end);});} 此时输入以下的结果 即Use与Run代码段都被执行了。需要注意的是管道中可以增加多个middleware他们是按顺序执行的执行的顺序与在Configure方法中代码的顺序是一致的。 三、Map与MapThen Map比较不同它将Middleware添加到管道中它是在管道中增加了分支。通过影射路径的方式增加管道分支。我们保留上面例子并增加代码。如下 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection(Logging));loggerFactory.AddDebug(); var loger loggerFactory.CreateLogger(TestLogger); //执行了app.Use (async (context,next) {loger.LogInformation(Use 1 start); await context.Response.WriteAsync(hello world!,Use ); await next.Invoke();loger.LogInformation(Use 1 end);});app.Map(/mapTest, HandleMap); //没的执行app.Run(async context {loger.LogInformation(run 1 start); await context.Response.WriteAsync( hello world!,run );loger.LogInformation(run 1 end);});} private static void HandleMap(IApplicationBuilder app){app.Run(async context { await context.Response.WriteAsync(Hello ,that is Handle Map );});} 运行起来我们在浏览器中输入” http://localhost:12716/mapTest” 得到的结果如下 mapTest分支被执行了。 MapThen就更有意思从字面上感觉有点类似查询的意思。对了。它就是处理符合条件的Request去执行给定的方法。我们修改代码 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory){loggerFactory.AddConsole(Configuration.GetSection(Logging));loggerFactory.AddDebug(); var loger loggerFactory.CreateLogger(TestLogger); //执行了app.Use (async (context,next) {loger.LogInformation(Use 1 start); await context.Response.WriteAsync(hello world!,Use ); await next.Invoke();loger.LogInformation(Use 1 end);});app.MapWhen(context { return context.Request.Query.ContainsKey(q); }, HandleQuery); //没的执行app.Run(async context {loger.LogInformation(run 1 start); await context.Response.WriteAsync( hello world!,run );loger.LogInformation(run 1 end);});} private static void HandleQuery(IApplicationBuilder app){app.Run(async context { await context.Response.WriteAsync( Hello ,this is Handle Query );});} 我们要处理的是:如果有URL中的参数包含了q字母的话就去执行HandleQuery方法。看一下结果 可以看出来MapWhen可以处理很多的东西比如我们要处理Request表头中某特定的内容可以使用MapWhen来处理。 相关文章 微软.NET 正式劈腿成功横跨所有平台.NET Core 1.0 CentOS7 尝试解读发布.NET Core RC2 and .NET Core SDK Preview 1[.NET Core].NET Core R2安装及示例教程ASP.NET Core 开发-中间件(Middleware)结合Jexus Kestrel 部署 asp.net core 生产环境通过Jexus 部署 dotnetcore版本MusicStore 示例程序ASP.NET Core 中文文档 第一章 入门用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序用 Visual Studio 和 ASP.NET Core MVC 创建首个 Web API用 Visual Studio 发布一个 Azure 云 Web 应用程序ASP.NET Core MVC 与 Visual Studio 入门第二章指南4.2添加 ControllerDotNet Core 介绍asp.net core 中间件详解及项目实战教你实践ASP.NET Core Authorization免看文档教程asp.net core 使用 Redis 和 Protobuf 进行 Session 缓存asp.net core 中间件详解及项目实战第二章 指南4.3添加 Viewdotnet core开发体验之开始MVCdotnet core 开发体验之Routing聊聊ASP.NET Core默认提供的这个跨平台的服务器——KestrelServer简析.NET Core 以及与 .NET Framework的关系.NET Core 使用Dapper 操作MySQL使用 CommandLineApplication 类创建专业的控制台程序简析 .NET Core 构成体系.NET Core也可以使用MongoDB了.NET Core ASP.NET Core 1.0在Redhat峰会上正式发布.NET Core面向未来的开源跨平台开发技术微软说它深爱着Linux现在它用行动证明了移植.NET Core计划整合各平台变得更简单了ASP.NET Core 介绍通过几个Hello World感受.NET Core全新的开发体验ASP.NET Core 运行原理剖析1:初始化WebApp模版并运行.NET Core系列 1、.NET Core 环境搭建和命令行CLI入门Asp.Net Core 发布和部署 MacOS Linux Nginx Asp.Net Core 发布和部署Linux Jexus 学习ASP.NET Core你必须了解无处不在的“依赖注入”.NET Core应用类型Portable apps Self-contained apps.NET Core 1.0发布微软开源跨平台大布局序幕ASP.NET Core 运行原理剖析2:Startup 和 Middleware(中间件)在Windows Server 2012 R2 Standard 部署 ASP.NET Core程序ASP.NET Core 开发-Entity Framework (EF) Core 1.0 Database First拥抱.NET Core跨平台的轻量级RPCRabbit.Rpc使用 dotnet watch 开发 ASP.NET Core 应用程序ASP.NET Core 发布至Linux生产环境 Ubuntu 系统ASP.NET Core Docker部署ASP.NET Core 完整发布,自带运行时 到jexus全球首发免费的MySql for Entity Framework Core 原文地址http://www.cnblogs.com/xiaoshou/p/5669122.html .NET社区新闻深度好文微信中搜索dotNET跨平台或扫描二维码关注