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

mas*_* ch 5 middleware 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
{
    public static IApplicationBuilder UseTest(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<TestMiddleware>();
    }
}
Run Code Online (Sandbox Code Playgroud)

Yin*_*qiu 5

您不能直接修改表单和查询字符串值,因为它们是只读的,您只能尝试替换它。

尝试更改您的中间件,如下所示:

public class TestMiddleware
{
    private readonly RequestDelegate _next;
    
    public TestMiddleware(RequestDelegate next)
    {
        _next = next;
        
    }

    public async Task Invoke(HttpContext httpContext)
    {
        var queryitems = httpContext.Request.Query.SelectMany(x => x.Value, (col, value) => new KeyValuePair<string, string>(col.Key, value)).ToList();
        List<KeyValuePair<string, string>> queryparameters = new List<KeyValuePair<string, string>>();
        foreach(var item in queryitems)
        {
            var value = item.Value.ToString().Replace("x", "y");
            KeyValuePair<string, string> newqueryparameter = new KeyValuePair<string, string>(item.Key, value);
            queryparameters.Add(newqueryparameter);                
        }
        
         var contentType = httpContext.Request.ContentType;

        if (contentType != null && contentType.Contains("multipart/form-data"))
        {
            var formitems = httpContext.Request.Form.SelectMany(x => x.Value, (col, value) => new KeyValuePair<string, string>(col.Key, value)).ToList();

            Dictionary<string, StringValues> formparameters = new Dictionary<string, StringValues>();
            foreach (var item in formitems)
            {
                var value = item.Value.ToString().Replace("x", "y");
                formparameters.Add(item.Key, value);
            };

            var qb1 = new QueryBuilder(queryparameters);
            var qb2 = new FormCollection(formparameters);
            httpContext.Request.QueryString = qb1.ToQueryString();
            httpContext.Request.Form = qb2;

            var items2 = httpContext.Request.Query.SelectMany(x => x.Value, (col, value) => new KeyValuePair<string, string>(col.Key, value)).ToList();
            var items3 = httpContext.Request.Form.SelectMany(x => x.Value, (col, value) => new KeyValuePair<string, string>(col.Key, value)).ToList();
        }        

        await _next(httpContext);

    }
}

public static class TestMiddlewareExtensions
{
    public static IApplicationBuilder UseTest(this IApplicationBuilder builder)
    {
        return builder.UseMiddleware<TestMiddleware>();
    }
}
Run Code Online (Sandbox Code Playgroud)

结果: 在此输入图像描述