如何通过IHttpActionResult获取内容

时间:2016-02-03 14:57:53

标签: c# asp.net-web-api asp.net-web-api2

我有一个简单的方法,它返回svc.coef_

IHttpActionResult

public IHttpActionResult invokeGetAction(HttpRequestMessage _request, String _forResource) { return new GetResourceActionResult(_request, _forResource); } 的实施如下:

GetResourceActionResult

我想调用System.Web.Http.ApiControllers的Get()方法中的public class GetResourceActionResult : IHttpActionResult { private readonly HttpRequestMessage _request; private readonly string _location; public GetResourceActionResult(HttpRequestMessage request, string location) { _request = request; _location = location; } public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken) { var response = _request.CreateResponse(HttpStatusCode.OK); response.Headers.Location = new Uri(_location); return Task.FromResult(response); } } 来将当前请求转发给另一个API,如

invokeGetAction()

我可以看到HttpStatusCode.OK,所以我想我现在没有失败。但我不知道在[Authorize] [Route("")] public IHttpActionResult Get() { return _someService.invokeGetAction(Request, "http://mockingsvc.../api/songs"); } 某处获取invokeGetAction()提供的内容数据的位置和方式?

1 个答案:

答案 0 :(得分:0)

好的是,我忘记了一些事情...... ;-)我的代码现在是

public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
    return Task.FromResult(Execute(_resourceUri));
}

public HttpResponseMessage Execute(String resourceUri)
    {
        using (var client = new HttpClient())
        {
            client.BaseAddress = new Uri(resourceUri);
            client.DefaultRequestHeaders.Accept.Clear();
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            return client.GetAsync("api/songs").Result;
        }
    }

它就像一个魅力。