这比我想象的容易。我在我的中间件是有纠正进来状态不佳的数据这样做。
public async Task Invoke(HttpContext context)
{
....
NameValueCollection fcnvc = context.Request.Form.AsNameValueCollection();
fcnvc.Set("a", "the correct value of a");
fcnvc.Set("b", "a value the client forgot to post");
Dictionary<string, StringValues> dictValues = new Dictionary<string, StringValues>();
foreach (var key in fcnvc.AllKeys)
{
dictValues.Add(key, fcnvc.Get(key));
}
var fc = new FormCollection(dictValues);
context.Request.Form = fc;
....
await _next.Invoke(context);
}
有趣的是,的FormCollection是只读的,但HttpContext.Request对象不是这样让我更换整个表格。
你是什么意思?“发送正确的HttpContext的方式?”你是否将它传递给另一个方法/对象,或者你是否试图将它发送回客户端? – greenjaed
我正在拦截传入的请求并修复中间件中的不良表单数据,并将良好的表单数据发送到未处理流水线的其余部分。 –