Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将HttpContext请求内容数据复制到新请求_C#_Asp.net Core_.net Core_Asp.net Core 2.0 - Fatal编程技术网

C# 将HttpContext请求内容数据复制到新请求

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

我正在为我们的dotnet核心微服务平台构建一个APIGateway代理

我用它作为起点,它通过使用

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);

另一方面,请确保使用。

查看代理中间件,以获取有关如何操作的示例