在ASP.NET 4.X应用中使用ASP.NET核心中间件
我有一个针对.NET标准2.0的库项目,其中asp.net核心中间件。它可以与针对.net 6的ASP.NET Core的应用程序正常工作。我希望能够在ASP.NET 4.X应用程序中使用相同的中间件/库。
是否有适配器类,或者我可以在库中创建一个适配类别,以允许4.x应用程序使用ASP.NET Core的中间件?因此,基本上,适配器将点击4.x应用程序的管道,并拦截匹配路径的HTTP请求。然后将这些调用传递到库中的ASP.NET核心中间件或业务逻辑层。
另外,我可以有两个不同的中间件。一个用于ASP.NET Core App(当前),另一个用于同一库中的ASP.NET 4.x应用程序?这两个中间件都将共享相同的后端库。
I have a library project targeting .NET Standard 2.0 with ASP.NET Core middleware. It works fine with apps targeting asp.net core to .NET 6. I like to be able to use the same middleware/library in asp.net 4.x apps.
Is there an adapter class or can I create one in the library that will allow 4.x apps to use the asp.net core's middleware? So basically the adapter will tap into 4.x app's pipeline and intercept the http request matching a path. Then pass those calls to asp.net core middleware or business logic layer in the library.
Alternatively, can I have two different middleware; one for asp.net core app (current) and another for asp.net 4.x apps in the same library? Both middleware will share the same backend libraries.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了使用ASP.NET MVC 4应用程序(我假设这是您的意思)并查看
请求
您需要与系统与httpcontext
进行交互.WEB库,在.NET标准中不可用。这有点过于简化,但是您可以在这里阅读更多。为了克服这个问题,一种选择是在3个Projests中容纳您的图书馆。业务逻辑项目将针对.NET标准2.0。带有.NET Core中间件的项目将针对.NET Core XX(OR .NET标准2.1),并引用业务逻辑项目。第三个项目将针对.NETFX 4.6+并参考您的业务逻辑项目。当然,这会产生2个单独的DLL/Nuget软件包,并且不是理想的,但可能是最简单的解决方案。
至于最初的问题,ASP.NET MVC 4没有中间件的概念。您可以添加
microsoft.owin.host.systemweb
软件包,将其引入MVC4应用程序。来自Microsoft的OWIN/MIDDERWARE的信息
但是,您将无法将MVC 4和.NET Core的同一中间件类重复使用,因为它们的依赖项和签名是不同的。它还需要更改您现有的MVC应用程序。
另一个选项是创建一个
ihttpmodule
,该将挂钩httpapplication
生命周期阶段最适合您的目的并在此处执行您的业务逻辑。您必须在
web.config
中注册模块:In order to work with an ASP.NET MVC 4 app (I'm assuming that's what you mean) and view a
Request
you need to interact with theHttpContext
from the System.Web library, which is not available in .Net Standard. This is a bit of an oversimplification, but you can read more here.To overcome this issue, one option would be to house your library across 3 projests. The business logic project would target .Net Standard 2.0. A project with your .Net Core middleware would target .Net Core x.x (or .Net Standard 2.1) and reference the business logic project. And a third project would target .NetFX 4.6+ and reference your business logic project. This of course produces 2 separate DLLs/NuGet packages and is less than ideal, but probably the easiest solution.
As for the original question, ASP.NET MVC 4 doesn't have the concept of middleware baked in. You can add the
Microsoft.Owin.Host.SystemWeb
package to introduce it into the MVC4 app.Information from Microsoft on OWIN/Middleware
However, you wouldn't be able to reuse the same middleware class for both MVC 4 and .Net Core as their dependencies and signatures are different. It also requires some altering of your existing MVC app.
Another option would be to create an
IHttpModule
that hooks into theHttpApplication
life cycle stage that best suits your purpose and executes your business logic there.You must register the module in your
web.config
: