小编mas*_* ch的帖子

如何修改asp.net core中间件的表单和查询字符串值?

我创建了一个中间件类。此类的目的是对所有表单和查询字符串值进行一些更改,但有一个问题!表单和查询字符串值是只读的,我无法更改它们。怎么办呢?

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)

middleware asp.net-core

5
推荐指数
1
解决办法
4795
查看次数

标签 统计

asp.net-core ×1

middleware ×1