我创建了一个新的 ASP.NET Core 8 MVC Web 应用程序,并向其添加了
Microsoft.AspNetCore.SystemWebAdapters
依赖项。
Program.cs
:
using OldHandlers;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Services.AddSystemWebAdapters(); // Register SystemWebAdapters
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
// Register SystemWebAdapters middleware
app.UseSystemWebAdapters();
// Define endpoints
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/myhandler", async context =>
{
var adapter = new AspNetCoreHttpContextAdapter(context);
var handler = new MyHandler();
handler.ProcessRequest(adapter);
});
});
app.UseAuthorization();
app.MapRazorPages();
app.Run();
我有:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SystemWebAdapters;
using System.Threading.Tasks;
using OldHandlers;
public class AspNetCoreHttpContextAdapter : IHttpContextAdapter
{
private readonly HttpContext _context;
public AspNetCoreHttpContextAdapter(HttpContext context)
{
_context = context;
}
public string ResponseContentType
{
get => _context.Response.ContentType;
set => _context.Response.ContentType = value;
}
public void WriteResponse(string content)
{
_context.Response.WriteAsync(content);
}
public async Task<string> ReadRequestBodyAsync()
{
using (StreamReader reader = new StreamReader(_context.Request.Body))
{
return await reader.ReadToEndAsync();
}
}
public void SetStatusCode(int statusCode)
{
_context.Response.StatusCode = statusCode;
}
public object GetRouteData(string key)
{
return _context.Items[key];
}
}
在我的旧网站中,我创建了一个新的处理程序:
public class MyHttpHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
var adapter = new SystemWebHttpContextAdapter(new HttpContextWrapper(context));
var handler = new MyHandler();
handler.ProcessRequest(adapter);
}
public bool IsReusable => false;
}
和:
using System.IO;
using System.Threading.Tasks;
using System.Web;
using OldHandlers;
public class SystemWebHttpContextAdapter : IHttpContextAdapter
{
private readonly HttpContextBase _context;
public SystemWebHttpContextAdapter(HttpContextBase context)
{
_context = context;
}
public string ResponseContentType
{
get => _context.Response.ContentType;
set => _context.Response.ContentType = value;
}
public void WriteResponse(string content)
{
_context.Response.Write(content);
}
public async Task<string> ReadRequestBodyAsync()
{
using (StreamReader reader = new StreamReader(_context.Request.InputStream))
{
return await reader.ReadToEndAsync();
}
}
public void SetStatusCode(int statusCode)
{
_context.Response.StatusCode = statusCode;
}
public object GetRouteData(string key)
{
return _context.Items[key];
}
}
我还创建了一个 .NET Standard 2.0 库来包含具体的处理程序实现:
namespace OldHandlers
{
public class MyHandler
{
public void ProcessRequest(IHttpContextAdapter context)
{
context.ResponseContentType = "text/plain";
context.WriteResponse("Hello, World!");
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace OldHandlers
{
public interface IHttpContextAdapter
{
string ResponseContentType { get; set; }
void WriteResponse(string content);
Task<string> ReadRequestBodyAsync();
void SetStatusCode(int statusCode);
object GetRouteData(string key);
}
}
我不明白的是,如果我使用
Microsoft.AspNetCore.SystemWebAdapters
,那么我是否需要创建我的适配器类AspNetCoreHttpContextAdapter
和SystemWebHttpContextAdapter
?
如果有人尝试 POC,我将不胜感激。我需要在旧的 asp.net 网站和新的 .NET 8 ASP.NET Core WebApp 之间共享 .netStandard 库。并且 HttpContext 应该得到共享库的支持。
以下是如何通过使用
HttpContext
的共享类库让 SystemWebAdapters
可供 ASP.NET 和 ASP.NET Core 使用的示例场景:
假设在旧的 ASP.NET MVC 项目(例如 .NET Framework 4.7.2)中,您有以下内容:
_Layout.cshtml
<small>@WebHelper.UserAgent</small>
WebHelper
存在于旧的类库中(例如.NET Framework 4.7.2),它依赖于System.Web
来确定用户代理:
using System.Web;
public class WebHelper
{
public static string UserAgent => HttpContext.Current.Request.UserAgent;
}
现在,
SystemWebAdapters
的核心是适配器库,它允许您使用.NET Standard中的常见System.Web
API(如HttpContext.Current
)。
为了演示,创建一个新的 .NET 标准类库并将上面显示的
WebHelper
类的代码复制到其中。要使 HttpContext.Current
工作,您需要引用 Microsoft.AspNetCore.SystemWebAdapters
NuGet 包。引用后您可以添加:
using System.Web;
即使类库项目面向 .NET Standard。
您现在可以从旧的 ASP.NET 和新的 ASP.NET Core 项目引用新的 .NET Standard 类库。确保您也引用了 ASP.NET Core 项目中的
Microsoft.AspNetCore.SystemWebAdapters
包。
希望有帮助。