[英]ASP.NET Core Modify HTTP Request Headers using Middleware
All I need to do is to modify [Connection]
HTTP Header from "Keep-alive" to lowercase "keep-alive". 我需要做的只是将
[Connection]
HTTP标头从“ Keep-alive”修改为小写的“ keep-alive”。
I wrote the class, 我写了课
public class PreRequestModifications
{
private readonly RequestDelegate _next;
public PreRequestModifications(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
// Does not get called when making an HTTPWebRequest.
await _next.Invoke(context);
}
}
and registered on startup, 并在启动时注册,
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseMiddleware<PreRequestModifications>();
}
but the Invoke
method does not get called when I execute await httpWebRequest.GetResponseAsync();
但是当我执行
await httpWebRequest.GetResponseAsync();
时,不会Invoke
方法await httpWebRequest.GetResponseAsync();
So middleware gets hit when a request is made and when a response is sent back. 因此,当发出请求并发送回响应时,中间件就会受到攻击。 Effectively this means you can move through the Invoke method twice like so:
实际上,这意味着您可以像下面这样两次遍历Invoke方法:
public async Task Invoke(HttpContext context)
{
ModifyRequest(context);
await _next(context);
ModifyResponse(context);
}
So you can modify the response in the ModifyResponse
method. 因此,您可以在
ModifyResponse
方法中修改响应。
Microsoft's documentation will make it clearer: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/middleware/?view=aspnetcore-2.1 Microsoft的文档将使其更加清晰: https : //docs.microsoft.com/zh-cn/aspnet/core/fundamentals/middleware/? view =aspnetcore-2.1
Hopefully this helps. 希望这会有所帮助。
Have you registered your middleware in the DI system? 您是否在DI系统中注册了中间件? You need to do so in your
Startup
class, ConfigureServices
method: 您需要在
Startup
类的ConfigureServices
方法中这样做:
services.AddScoped<IMiddleware, SomeMiddleware>();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.