了解ASP.NET 5中间件的实践用法中间件、ASP、NET

2023-09-04 08:19:23 作者:沸腾味道

这伟大的文章解释了有关 ASP.NET 5中间件,或者有了我的HttpModule到哪里去了?

This great article explains things about ASP.NET 5 Middleware, Or Where Has My HttpModule Gone?

不过,目前还不清楚什么时候,为什么我们要使用 ASP.NET 5中间件

But it is still unclear about when and why we have to use ASP.NET 5 Middleware.

任何人都可以解释它,并提供其用法的现实生活中的例子吗?

Can anyone explain it and provide real life examples of its usage?

推荐答案

这其实很简单。您可以创建一个中间件得到保留的请求,并决定:

It's actually quite simple. You would create a middleware to get a hold of a request and decide:

无论你是想处理它,并把它传递给下一个中间件。 您是否要对其进行处理,生成响应,减少请求生命周期存在。

您也可以有一个中间件作用于响应仅(如中间件谁做的COM pression)。

You can also have a middleware to act on the responses only (e.g. a middleware who does the compression).

大多数中间件都提供跨领域的功能,如路由,认证,COM pression,错误处理。在这几个真实的例子:

Most of the middlewares are to provide cross cutting functionality such as routing, authentication, compression, error handling. A few real world examples on these:

ASP.NET 5路由中间件 ASP.NET 5认证的中间件 ASP.NET 5错误处理程序中间件 ASP.NET 5 Routing Middleware ASP.NET 5 Authentication Middlewares ASP.NET 5 Error Handler Middleware

一个混乱可能约为framworks这里,如MVC,SignalR等。previously的武士刀的世界里,每一个框架在创建每个自己的中间件挂接到系统中。在ASP.NET 5,这是一个有点不同。为了建立一个框架来处理请求,你可以通过提供一个特殊的处理程序挂接到路由中间件。例如,这里是 MVC 6路由处理 。当你看biuilder扩展,您将看到MVC实际使用的路由中间件。

One confusion could be around framworks here such as MVC, SignalR, etc.. Previously in Katana world, each framework was creating each own middleware to hook into the system. With ASP.NET 5, this is a little different. In order for a framework to process requests, you can hook into the routing middleware by providing a special handler. For example, here is the MVC 6 route handler. When you look at the biuilder extensions, you will see that MVC actually uses the routing middleware.