Asp.net core 如何修改表格&;asp.net核心中中间件的查询字符串值?

Asp.net core 如何修改表格&;asp.net核心中中间件的查询字符串值?,asp.net-core,middleware,Asp.net Core,Middleware,我创建了一个中间件类。此类的目的是对所有表单和查询字符串值进行一些更改,但存在一个问题!表单和查询字符串值是只读的,我无法更改它们。你怎么能做到 public class TestMiddleware { private readonly RequestDelegate _next; public TestMiddleware(RequestDelegate next) { _next = next; } public async Ta

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

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>();
    }
}
公共类测试中间件
{
private readonly RequestDelegate\u next;
公共测试中间件(RequestDelegate next)
{
_下一个=下一个;
}
公共异步任务调用(HttpContext HttpContext)
{
var collection=httpContext.Request.Query;
foreach(集合中的var项)
{
集合[item.Key]=item.Value.ToString().Replace('x','y');
}
httpContext.Request.Query=集合;
var collection2=httpContext.Request.Form;
foreach(集合2中的var项目)
{
collection2[item.Key]=item.Value.ToString().Replace('x','y');
}
httpContext.Request.Form=collection2;
等待下一步(httpContext);
}
}
//用于将中间件添加到HTTP请求管道的扩展方法。
公共静态类TestMiddlewareExtensions
{
公共静态IAApplicationBuilder UseTest(此IAApplicationBuilder)
{
返回builder.UseMiddleware();
}
}

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

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

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>();
    }
}
公共类测试中间件
{
private readonly RequestDelegate\u next;
公共测试中间件(RequestDelegate next)
{
_下一个=下一个;
}
公共异步任务调用(HttpContext HttpContext)
{
var queryitems=httpContext.Request.Query.SelectMany(x=>x.Value,(col,Value)=>newkeyvaluepair(col.Key,Value)).ToList();
List queryparameters=新列表();
foreach(查询项中的变量项)
{
var value=item.value.ToString().替换(“x”,“y”);
KeyValuePair newqueryparameter=新的KeyValuePair(item.Key,value);
添加(newqueryparameter);
}
var contentType=httpContext.Request.contentType;
if(contentType!=null&&contentType.Contains(“多部分/表单数据”))
{
var formitems=httpContext.Request.Form.SelectMany(x=>x.Value,(col,Value)=>newkeyvaluepair(col.Key,Value)).ToList();
Dictionary formparameters=新字典();
foreach(表单中的var项目)
{
var value=item.value.ToString().替换(“x”,“y”);
formparameters.Add(item.Key,value);
};
var qb1=新的查询生成器(查询参数);
var qb2=新FormCollection(formparameters);
httpContext.Request.QueryString=qb1.ToQueryString();
httpContext.Request.Form=qb2;
var items2=httpContext.Request.Query.SelectMany(x=>x.Value,(col,Value)=>newkeyvaluepair(col.Key,Value)).ToList();
var items3=httpContext.Request.Form.SelectMany(x=>x.Value,(col,Value)=>newkeyvaluepair(col.Key,Value)).ToList();
}        
等待下一步(httpContext);
}
}
公共静态类TestMiddlewareExtensions
{
公共静态IAApplicationBuilder UseTest(此IAApplicationBuilder)
{
返回builder.UseMiddleware();
}
}
结果:

感谢您的回复,但我遇到了这个错误:处理请求时发生了未经处理的异常。InvalidOperationException:不正确的内容类型:Microsoft.AspNetCore.Http.Features.FormFeature.ReadForm()var formitems=httpContext.Request.Form.SelectMany(x=>x.Value,(col,Value)=>new KeyValuePair(col.Key,Value)).ToList();屏幕截图:由于获取表单需要表单数据类型,因此当程序启动时,如果没有相应的内容类型,程序将报告错误。因此,您需要在获取表单之前添加判断,我已更新了我的答案。我建议将application/x-www-form-urlencoded添加到此条件