Asp.net core 将asp.net核心控制器的POST请求转发到不同的URL

Asp.net core 将asp.net核心控制器的POST请求转发到不同的URL,asp.net-core,http-post,httprequest,Asp.net Core,Http Post,Httprequest,我想将传入的POST请求“按原样”(包括标题、正文、来自数据)转发到不同的URL,而无需使用中间件 我找到了一个在asp.net中执行此操作的示例: 但这不适用于asp.net核心,因为调用 返回wait http.sendaync(this.Request); 在asp.net中,core接受HttpRequestMessage,请求对象的类型为HttpRequest 我还发现了一些代码,可以从HttpRequest创建HttpRequestMessage,请参见: 使用此代码,接收端点(我转

我想将传入的POST请求“按原样”(包括标题、正文、来自数据)转发到不同的URL,而无需使用中间件

我找到了一个在asp.net中执行此操作的示例:

但这不适用于asp.net核心,因为调用 返回wait http.sendaync(this.Request); 在asp.net中,core接受HttpRequestMessage,请求对象的类型为HttpRequest

我还发现了一些代码,可以从HttpRequest创建HttpRequestMessage,请参见:

使用此代码,接收端点(我转发到的端点)获取主体,但不获取表单字段

检查HttpRequestMessage类时,我发现它不包含FormFields的属性

[Microsoft.AspNetCore.Mvc.HttpPost]
[NrgsRoute("api/redirect-v1/{key}")]
public async Task<HttpResponseMessage> Forward(
   [FromUri] string key,
   CancellationToken cancellationToken)
{
   // the URL was shortened, we need to get the original URL to which we want to forward the POST request
   var url = await _shortenUrlService.GetUrlFromToken(key, cancellationToken).ConfigureAwait(false);

     using (var httpClient = new HttpClient())
     {
         var forwardUrl = new Uri(url);
         Request.Path = new PathString(forwardUrl.PathAndQuery);

         // see: https://stackoverflow.com/questions/45759417/convert-microsoft-aspnetcore-http-httprequest-to-httprequestmessage
         var requestMessage = Request.ToHttpRequestMessage();
         return await httpClient.SendAsync(requestMessage, cancellationToken);

         // Problem: Forwards header and body but NOT form fields
     }
 }
[Microsoft.AspNetCore.Mvc.HttpPost]
[NrgsRoute(“api/redirect-v1/{key}”)]
公共异步任务转发(
[FromUri]字符串键,
取消令牌(取消令牌)
{
//URL被缩短了,我们需要得到我们想要转发POST请求的原始URL
var url=await\u shortenUrlService.GetUrlFromToken(key,cancellationToken).ConfigureAwait(false);
使用(var httpClient=new httpClient())
{
var forwardUrl=新Uri(url);
Request.Path=新路径字符串(forwardUrl.PathAndQuery);
//见:https://stackoverflow.com/questions/45759417/convert-microsoft-aspnetcore-http-httprequest-to-httprequestmessage
var requestMessage=Request.ToHttpRequestMessage();
返回等待httpClient.SendAsync(requestMessage,cancellationToken);
//问题:转发标题和正文,但不转发表单字段
}
}
预期的结果是,在我的接收端点,我有相同的结果 -标题 -身体 -表单字段
与最初的POST请求一样。

我最后做了以下几件事:

[HttpPost]
[NrgsRoute(“api/redirect-v1/{key}”)]
公共异步任务转发(字符串键,CancellationToken CancellationToken)
{
var url=await\u shortenUrlService.GetUrlFromToken(key,cancellationToken).ConfigureAwait(false);
if(string.IsNullOrEmpty(url))
抛出新的BadRequestException($“无法从参数{key}”创建转发URL”,“重定向错误”);
使用(var httpClient=new httpClient())
{
var forwardUrl=新Uri(url);
Request.Path=新路径字符串(forwardUrl.PathAndQuery);
HttpResponseMessage-responseMessage;
if(Request.HasFormContentType)
responseMessage=Wait ForwardFormData(密钥、httpClient、forwardUrl、cancellationToken);
其他的
responseMessage=Wait ForwardBody(密钥、httpClient、cancellationToken);
var queryParams=forwardUrl.GetQueryStringParams();
var lUlr=查询参数[“lurl”];
返回新的重定向结果(lUlr);
}
}
专用异步任务ForwardFormData(字符串键、HttpClient HttpClient、Uri forwardUrl、CancellationToken CancellationToken)
{
var formContent=新列表();
HttpResponseMessage结果;
if(Request.ContentType==“application/x-www-form-urlencoded”)
{
foreach(Request.Form.Keys中的var formKey)
{
var content=Request.Form[formKey].FirstOrDefault();
如果(内容!=null)
添加(新的KeyValuePair(formKey,content));
}
var formUrlEncodedContent=新的formUrlEncodedContent(formContent);
结果=等待httpClient.PostAsync(forwardUrl、formUrlEncodedContent、cancellationToken);
}
其他的
{
var multipartFormDataContent=新的multipartFormDataContent();
foreach(Request.Form.Keys中的var formKey)
{
var content=Request.Form[formKey].FirstOrDefault();
如果(内容!=null)
multipartFormDataContent.Add(新的StringContent(内容),formKey);
}
结果=等待httpClient.PostAsync(forwardUrl、multipartFormDataContent、cancellationToken);
}
返回结果;
}
专用异步任务转发体(字符串密钥、HttpClient HttpClient、CancellationToken CancellationToken)
{
//我们无法直接访问内容,请参阅:https://stackoverflow.com/questions/41508664/net-core-forward-a-local-api-form-data-post-request-to-remote-api
var requestMessage=Request.ToHttpRequestMessage();
返回等待httpClient.SendAsync(requestMessage,cancellationToken);
}

感谢您的问答!你救了我一天!