我正在编写一个中间层服务,对于上下文,它会将请求转发给外部团队拥有的“秘密”网址,并对请求执行auth / authZ。
我正在寻找一种方法将for
直接返回给调用者,这样两个响应对象(我收到的那个,以及我接收到的API的一个调用者)看起来都是一样的(或尽可能接近)
我的API定义如此
HttpResponseMessage
我已经尝试过回复 [Route("proxy/{input1}")]
[Authorize(Policy = "Admin")]
public async Task/*<SomeType>*/ ProxyCall(string input1)
{
var client = new HttpClient();
var request = httpContextAccessor.HttpContext?.Request;
var uriToCall = QueryHelpers.AddQueryString(LookupUri(input1), request.Query.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.ToString()));
var httpContent = new StreamContent(request.Body);
httpContent.Headers.ContentType = new MediaTypeHeaderValue(request.ContentType);
httpContent.Headers.ContentLength = request.ContentLength;
var executionRequest = new HttpRequestMessage(new HttpMethod(GetMethodForHost(uriToCall)), uriToCall) { Content = httpContent };
var response = await client.SendAsync(executionRequest);
/* I now have an HttpResponseMessage object. I want this response object
to be returned as is*/
/*
// This returns a json object which is the HttpRequestMessage serialized to json, with 200 status code
return response;
*/
/*
// This looks close, but the body is always empty
var responseStream = await result.Content.ReadAsStreamAsync();
Response.Body = responseStream;
Response.StatusCode = (int)result.StatusCode;
foreach(var header in result.Headers)
{
Response.Headers.Add(header.Key, new StringValues(header.Value.ToArray()));
}
*/
}
等等。但是我最喜欢的是看到一种简单,优雅的方式来返回消息响应,状态代码,标题,内容等包括在内