C# 将HttpContext请求内容数据复制到新请求
我正在为我们的dotnet核心微服务平台构建一个APIGateway代理 我用它作为起点,它通过使用C# 将HttpContext请求内容数据复制到新请求,c#,asp.net-core,.net-core,asp.net-core-2.0,C#,Asp.net Core,.net Core,Asp.net Core 2.0,我正在为我们的dotnet核心微服务平台构建一个APIGateway代理 我用它作为起点,它通过使用 app.Run(async (context) => { // Do things with context }); 您拥有到网关的请求的上下文,但是如何将内容数据从网关请求复制到我将向API发出的新请求 我看到了将请求内容设置为HttpContent对象的功能: newRequest.Content = new StringContent(requestContent, Enco
app.Run(async (context) =>
{
// Do things with context
});
您拥有到网关的请求的上下文,但是如何将内容数据从网关请求复制到我将向API发出的新请求
我看到了将请求内容设置为HttpContent对象的功能:
newRequest.Content = new StringContent(requestContent, Encoding.UTF8, "application/json");
但是我希望我的应用程序通过网关上传文件,我发现唯一的方法是创建MultipartFormDataContent,但是所有关于如何创建MultipartFormDataContent的示例都使用一个IFormFile而不是HttpContext
是否有办法将初始apigateway请求上的内容复制到我的内部请求:
using (var newRequest = new HttpRequestMessage(new HttpMethod(request.Method), serviceUrl))
{
// Add headers, etc
newRequest.Content = // TODO: how to get content from HttpContext
using (var serviceResponse = await new HttpClient().SendAsync(newRequest))
{
// handle response
}
}
为此,您可以将HttpContext.Request.Body
流
作为要使用的实际内容传入。下面是您的示例中的情况:
newRequest.Content = new StreamContent(context.Request.Body);
另一方面,请确保使用。查看代理中间件,以获取有关如何操作的示例