从ASP.NET Core中的传递API返回HttpResponseMessage内容

时间:2018-05-31 02:03:37

标签: c# asp.net-core asp.net-core-2.0 asp.net-core-webapi

我正在编写一个中间层服务,对于上下文,它会将请求转发给外部团队拥有的“秘密”网址,并对请求执行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())); } */ } 等等。但是我最喜欢的是看到一种简单,优雅的方式来返回消息响应,状态代码,标题,内容等包括在内

0 个答案:

没有答案