I have Created a middleware class. The purpose of this class is to make some changes in all form and querystring values but there is a problem! form and querystring values are readonly and I can not change them. how can do it?
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
{
public static IApplicationBuilder UseTest(this IApplicationBuilder builder)
{
return builder.UseMiddleware<TestMiddleware>();
}
}
