ASP.NET Core - 中间件

ASP.NET Core - 中间件 首页 / ASP.Net Core入门教程 / ASP.NET Core - 中间件

在本章中,无涯教程将了解如何设置中间件(Middleware),ASP.NET Core中间件控制应用程序如何响应HTTP请求。

现在假设想将有关每个请求的信息记录到应用程序中。

  • 在这种情况下,可能会安装到应用程序中的第一个中间件是日志记录(Logger)组件。

  • 该记录器(Logger)可以看到有关传入请求的所有信息,但是记录器很可能只是记录一些信息,然后将该请求传递给下一个中间件。

Middleware
  • 中间件是此处理管道中存在的一系列组件。

  • 安装到应用程序中的下一个中间件是授权者(Authorize)。

  • 授权者(Authorize)可能正在HTTP标头中查找特定的cookie或访问令牌。

  • 如果授权者找到令牌,它将允许请求继续进行,否则,授权者可能会使用HTTP错误代码或重定向代码来响应请求,以将用户发送到登录页面。

  • 授权者会将请求传递给下一个中间件,即路由器(Router)。

  • 路由器(Router)会查看URL并确定下一步的操作。

    链接:https://www.learnfk.comhttps://www.learnfk.com/asp.net_core/asp.net-core-middleware.html

    来源:LearnFk无涯教程网

  • 路由器(Router)会在应用程序中寻找要响应的内容,如果路由器未找到要响应的内容,则路由器本身可能会返回 404 Not Found错误。

现在举一个简单的示例来了解有关中间件的更多信息,使用 Startup类的Configure方法在ASP.NET中设置中间件。

using Microsoft.AspNet.Builder; 
using Microsoft.AspNet.Hosting; 
using Microsoft.AspNet.Http; 

using Microsoft.Extensions.DependencyInjection; 
using Microsoft.Extensions.Configuration;  

namespace FirstAppDemo { 
   public class Startup { 
      public Startup() { 
         var builder = new ConfigurationBuilder() 
            .AddJsonFile("AppSettings.json"); 
         Configuration = builder.Build(); 
      }  
      public IConfiguration Configuration { get; set; }  
       
      //This method gets called by the runtime. 
      //Use this method to add services to the container. 
      //For more information on how to configure your application, 
      //visit http://go.microsoft.com/fwlink/?LinkID=398940 
      public void ConfigureServices(IServiceCollection services) { 
      }  
      
      //This method gets called by the runtime.  
      //Use this method to configure the HTTP request pipeline. 
      public void Configure(IApplicationBuilder app) { 
         app.UseIISPlatformHandler();  
         
         app.Run(async (context) => { 
            var msg = Configuration["message"]; 
            await context.Response.WriteAsync(msg); 
         });  
      }  
      //Entry point for the application. 
      public static void Main(string[] args) => WebApplication.Run<Startup>(args); 
   } 
} 

在 Configure()方法内部,将在IApplicationBuilder接口上调用扩展方法以添加中间件。

默认情况下,一个新的空项目中有两个中间件-

  • IISP平台处理程序
  • 向app.Run注册的中间件

IISPlatformHandler允许使用Windows身份验证,它将查看每个传入的请求并查看是否有与该请求关联的Windows身份信息,然后调用下一个中间件。

注册的中间件

下一个中间件是在 app.Run 中注册的中间件, Run方法允许传入另一个方法,该方法可用于处理每个单个响应,它被称为中间件的终端。

您还可以访问Response对象,并且使用Response对象可以做的一件事情就是写一个字符串。

如果要在app.Run之后注册另一个中间件,则永远不会调用该中间件,因为Run是中间件的终端,它永远不会调用下一个中间件。

无涯教程网

添加另一个中间件

继续以下步骤以添加另一个中间件-

步骤1 - 要添加其他中间件,请右键单击项目,然后选择"Manage NuGet Packages"。

步骤2 - 搜索 Microsoft.aspnet.diagnostics ,它实际上是ASP.NET Core中间件,用于异常处理,异常显示页面和诊断信息,这个特定的软件包包含许多无涯教程可以使用的中间件。

Microsoft AspNet Diagnostics

步骤3 - 如果您的项目中未安装该软件包,请安装该软件包。

步骤4 - 现在转到 Configure()方法并调用 app.UseWelcomePage 中间件。

//This method gets called by the runtime.  
//Use this method to configure the HTTP request pipeline. 
public void Configure(IApplicationBuilder app) { 
   app.UseIISPlatformHandler(); 
   app.UseWelcomePage();  
   
   app.Run(async (context) => { 
      var msg = Configuration["message"]; 
      await context.Response.WriteAsync(msg); 
   });  
}

步骤5 - 运行您的应用程序,您将看到以下欢迎屏幕。

Welcome Screen

此欢迎屏幕可能没有用。

步骤6 - 尝试一些可能更有用的方法,将使用 RuntimeInfoPage 而不是使用Welcome页面。

//This method gets called by the runtime.  
//Use this method to configure the HTTP request pipeline. 
public void Configure(IApplicationBuilder app) { 
   app.UseIISPlatformHandler(); 
   app.UseRuntimeInfoPage();  
   
   app.Run(async (context) => { 
      var msg = Configuration["message"]; 
      await context.Response.WriteAsync(msg); 
   });  
}

步骤7 - 保存 Startup.cs 页面并刷新浏览器,您将看到以下页面。

Startup CS

此RuntimeInfoPage是一个中间件,它将仅响应针对特定URL的请求,如果传入的请求与该URL不匹配,则此中间件只是让请求传递到下一个中​​间件,该请求将通过IISPlatformHandler中间件,然后转到UseRuntimeInfoPage中间件,它不会创建响应,因此它将转到应用程序,运行并显示字符串。

步骤8 - 让您的URL末尾添加"/runtimeinfo "。现在,您将看到由该运行时信息页面中间件生成的页面。

Runtime Information

现在,您将看到一个响应,该响应为您提供了有关运行时环境的一些信息,如操作系统,运行时版本,体系结构,类型以及您正在使用的所有软件包等。

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

赵成的运维体系管理课 -〔赵成〕

说透敏捷 -〔宋宁〕

正则表达式入门课 -〔涂伟忠〕

攻克视频技术 -〔李江〕

深入C语言和程序运行原理 -〔于航〕

反爬虫兵法演绎20讲 -〔DS Hunter〕

手把手带你搭建推荐系统 -〔黄鸿波〕

结构思考力 · 透过结构看问题解决 -〔李忠秋〕

程序员职业规划手册 -〔雪梅〕

好记忆不如烂笔头。留下您的足迹吧 :)