我创建了一个中间件类。此类的目的是对所有表单和查询字符串值进行一些更改,但有一个问题!表单和查询字符串值是只读的,我无法更改它们。怎么办呢?
public class TestMiddleware
{
private readonly RequestDelegate _next;
public TestMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
var collection = httpContext.Request.Query;
foreach (var item in collection)
{
collection[item.Key] = item.Value.ToString().Replace('x', 'y');
}
httpContext.Request.Query = collection;
var collection2 = httpContext.Request.Form;
foreach (var item in collection2)
{
collection2[item.Key] = item.Value.ToString().Replace('x', 'y');
}
httpContext.Request.Form = collection2;
await _next(httpContext);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class TestMiddlewareExtensions …Run Code Online (Sandbox Code Playgroud)