如何构建自定义中间件在ASP。网络核心

利用ASP。网络核心的中间件to customize your application’s handling of HTTP requests and responses.

  • 在Facebook上分享
  • 在Twitter上分享
  • 分享在LinkedIn
  • 在Reddit分享
  • 通过电子邮件分享
  • 印刷资源
如何构建自定义中间件在ASP。网络核心
思想库

ASP。网的核心是一个开源、跨平台,精益,模块化构建高性能的web应用程序的框架。它也是可扩展的。当构建一个ASP。网络核心application, you can draw on various middleware components to customize the handling of requests and responses, and even inspect, route, or modify the request and response messages that flow through the pipeline. This article presents a discussion of ASP.NET Core middleware and how it can be used, with relevant code examples in C#.

通常,你有一个中间件组件链在ASP应用程序的管道。净的核心。ASP。网络核心request pipeline contains a series of request delegates that are invoked one after the other. Incoming requests flow through each of the middleware components in the pipeline, and each of these components can either process the request or pass the request to the next component in the pipeline.

配置ASP。网络核心的中间件pipeline

中间件使用管道配置配置启动的方法。cs文件。这是哪里你可以一起链ASP。网络核心管道。自动配置方法是由ASP调用。网运行时。这是一个快速浏览一下这个方法。

公共空间配置(IApplicationBuilder应用,IHostingEnvironment env, ILoggerFactory loggerFactory, IdentityDbContext dbContext)
{
app.UseDeveloperExceptionPage ();
app.UseStaticFiles ();
app.UseMvcWithDefaultRoute ();
}

上面的代码片段演示了如何将MVC添加到ASP。网络核心请求执行管道。在下一节中,我们将构建一个定制的中间件组件为ASP。净的核心。

创建一个ASP。网络核心project in Visual Studio

假设你有。net核心安装在您的系统中(最好是。net核心2),您可以按照以下步骤创建一个新的ASP。网络核心项目在Visual Studio 2017和Visual Studio 2015。

  1. 打开Visual Studio
  2. 单击文件- >新建- >项目
  3. 在新项目对话框窗口中,选择ASP。网络核心Web应用程序项目模板
  4. 指定的名称和位置为您的项目,然后单击OK保存
  5. 在接下来的屏幕中,从列表中选择Web API的模板,取消选择身份验证和码头工人支持(目前我们不需要这两种),并保存项目

这是所有您需要做的创建一个ASP。网络核心web application that leverages Web API. We will now build a custom middleware component that checks if the authentication key passed in the request is valid. If the authentication key is not valid, the request will be rejected with an appropriate message.

创建一个定制的ASP。网络核心的中间件component

你有一个扩展方法IApplicationBuilder你可以利用在ASP构建自己的中间件。净的核心。注意,它总是一个很好的实践编写中间件组件在一个单独的类或者一个单独的项目,这样您就可以更新中间件如果需要,而无需改变你的服务层。

有多种方法来构建一个自定义中间件部分将演示的最简单的方法。在解决方案资源管理器窗口中右键单击该项目,并创建一个新的名为MyCustomMiddleware cs文件。乍一看,这个类是这样的。

公开课MyCustomMiddleware
{
/ /这是我们定制的中间件
}

请注意,您的自定义中间件应该有一个构造函数的参数类型RequestDelegate。一个RequestDelegate只是一个代表,接受一个吗HttpContext并返回一个任务。这是我们自定义的修改版本中间件应该。

公开课MyCustomMiddleware
{
私人只读的RequestDelegate _next;
公共MyCustomMiddleware (RequestDelegate下)
{
_next =下一个;
}
}

调用定制ASP。网络核心的中间件

您的中间件也应该有一个方法时,将调用执行中间件。这是调用方法如以下代码清单所示。注意,这个方法已经被命名为调用在这个示例中只按照惯例。在这里,您可以编写代码来检查或修改请求和响应。

公开课MyCustomMiddleware
{
私人只读的RequestDelegate _next;
公共MyCustomMiddleware (RequestDelegate下)
{
_next =下一个;
}
公共任务异步调用(HttpContext HttpContext)
{
如果(! httpContext.Request.Headers.Keys。
包含(“认证密钥”))
{
httpContext.Response。StatusCode = 400;
等待httpContext.Response.WriteAsync
(“认证密钥丢失…”);
返回;
}
其他的
{
/ /编写代码来验证认证密钥。
}
等待_next.Invoke (httpContext);
}
}

参考上面给出的代码清单。如果请求头不包含一个有效的认证密钥,然后一个适当的消息将被作为响应返回当您运行您的Web API。(注意,“认证密钥”是一个字符串,而对有效键的列表。)

创建一个扩展方法,ASP。网络核心的中间件

现在让我们创建一个扩展方法为我们定制中间件。扩展方法的目的是,让它更容易注册中间件的管道。ASP。净团队建议暴露IApplicationBuilder扩展的方法,可以调用配置方法。这是如何构建一个扩展方法,它利用了IApplicationBuilder

公共静态类MyCustomMiddlewareExtensions
{
公共静态IApplicationBuilder UseMyCustomMiddleware
(这IApplicationBuilder builder)
{
返回builder.UseMiddleware < MyCustomMiddleware > ();
}
}

最后,你应该配置定制的中间件配置方法如下所示。然后您可以使用您的扩展方法,例如:UseMyCustomMiddleware ()下面的代码片段所示。

公共空间配置(IApplicationBuilder应用,IHostingEnvironment env)
{
app.UseMyCustomMiddleware ();
}

中间件在ASP。网络核心allows us to create very flexible web applications. Our authentication key example is just one of many ways we might take advantage of middleware. You can learn more about ASP.NET Core middleware from本文在ASP。网络核心文档

版权©2018 IDG通信公司。

奖2023年信息世界的技术。现在打开条目!