简体   繁体   English

如何为 ASP.NET WebForms、MVC 和 CORE 拥有一个通用的 HttpContext

[英]How to have a common HttpContext for ASP.NET WebForms, MVC and CORE

I'm working on a Nuget library which can be used for web applications (WebForms, MVC, CORE) and will be published for both .NetFramework and .NetStandard.我正在开发一个 Nuget 库,该库可用于 Web 应用程序(WebForms、MVC、CORE),并将为 .NetFramework 和 .NetStandard 发布。

In my library I need to access the HttpContext for reading the Request and also for redirecting and writing in the Response object.在我的库中,我需要访问 HttpContext 以读取请求以及重定向和写入响应对象。

What's the best way to have a common HttpContext for all three frameworks WebForms, MVC and CORE?为所有三个框架 WebForms、MVC 和 CORE 拥有通用 HttpContext 的最佳方法是什么?

Note that Microsoft.Owin is not compatible with .NetStandard and also in WebForms and MVC there is a System.Web.HttpContextBase class which is completely different in .NETCore projects and there we have Microsoft.AspNetCore.Http.HttpContext.请注意,Microsoft.Owin 与 .NetStandard 不兼容,而且在 WebForms 和 MVC 中有一个 System.Web.HttpContextBase 类,它在 .NETCore 项目中完全不同,我们有 Microsoft.AspNetCore.Http.HttpContext。

Your library could be organized this way:你的图书馆可以这样组织:

  1. Define adapter interfaces (over HTTP context and etc ) in shared source project共享源项目中定义适配器接口(通过HTTP 上下文等
  2. Define implementations of all those interface in concreate Core or Classic project - create both.在 concreate Core 或 Classic 项目中定义所有这些接口的实现 - 创建两者。 There you should have 3 projects in your solution.您的解决方案中应该有 3 个项目。
  3. Now your library is 4th project it should be multitarget , use conditional symbols to enable or disable "Classic" or "Core" adapters.现在您的库是第 4 个项目,它应该是multitarget ,使用条件符号来启用或禁用“经典”或“核心”适配器。 Use adapters to access " Http Context and etc ".使用适配器访问“ Http Context 等”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM