如何设置Web API为路由代理控制器? [英] How to set up Web API Routing for a Proxy Controller?

查看:210
本文介绍了如何设置Web API为路由代理控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序的一部分需要充当代理服务器为第三方RESTful Web服务。有没有一种方法来设置Web API路由,以便相同类型的所有请求都将同样的方法?

Part of my application needs to act as a Proxy Server for a third party RESTful web service. Is there a way to set up Web API routing so that all requests of the same type will go to the same method?

例如,如果客户端在这两种GET请求我希望他们去到一个单一的GET操作方法然后请求到下游服务器发送。

For example, if the client sends in either of these GET requests I want them to go into a single GET action method that then sends on the request to the downstream server.

api/Proxy/Customers/10045
api/Proxy/Customers/10045/orders
api/Proxy/Customers?lastname=smith

有关GET便拿起这三个要求中的任何一个,并送他们到各自的服务(我知道如何与HttpClient的合作,使有效地发生)单操作方法:

The single action method for GET would pick up any one of these three requests and send them on to the respective service (I know how to work with HttpClient to make that happen effectively):

http://otherwebservice.com/Customers/10045
http://otherwebservice.com/Customers/10045/orders
http://otherwebservice.com/Customers?lastname=smith

我不希望有紧密结合我的web服务的第三方网络服务并复制其整个API的方法中调用矿

I don't want to have to tightly couple my web service to the third party web service and replicate their entire API as method calls inside mine.

这是我想到的一个解决办法是简单地连接code目标URL在JavaScript中的客户端上,并传递到Web API,然后将只能看到一个参数这一点。它的工作,但我想preFER在网页API是否可以使用路由功能。

One workaround that I have thought of is to simply encode the target URL in JavaScript on the client and pass this into the Web API which will then only see one parameter. It would work, but I'd prefer to use the routing capabilities in Web API if possible.

推荐答案

下面就是我得到了这个工作。首先,创建一个控制器对每个动词的方法你想支持:

Here's how I got this to work. First, create a controller with a method for each verb you want to support:

public class ProxyController : ApiController
{
    private Uri _baseUri = new Uri("http://otherwebservice.com");

    public async Task<HttpResponseMessage> Get(string url)
    {
    }

    public async Task<HttpResponseMessage> Post(string url)
    {
    }

    public async Task<HttpResponseMessage> Put(string url)
    {
    }

    public async Task<HttpResponseMessage> Delete(string url)
    {
    }
}

该方法是异步,因为他们将使用一个HttpClient的。地图路线是这样的:

The methods are async because they're going to use an HttpClient. Map your route like this:

config.Routes.MapHttpRoute(
  name: "Proxy",
  routeTemplate: "api/Proxy/{*url}",
  defaults: new { controller = "Proxy" });

现在回到在控制器Get方法。创建一个HttpClient的对象,创建相应的URL新的Htt prequestMessage对象,复制一切(或几乎所有)从原来的请求消息,然后调用SendAsync():

Now back to the Get method in the controller. Create an HttpClient object, create a new HttpRequestMessage object with the appropriate Url, copy everything (or almost everything) from the original request message, then call SendAsync():

public async Task<HttpResponseMessage> Get(int tenantId, string url)
{
    using (var httpClient = new HttpClient()
    {
        string absoluteUrl = _baseUri.ToString() + "/" + url + Request.RequestUri.Query;
        var proxyRequest = new HttpRequestMessage(Request.Method, absoluteUrl);
        foreach (var header in Request.Headers)
        {
            proxyRequest.Headers.Add(header.Key, header.Value);
        }

        return await httpClient.SendAsync(proxyRequest);
    }
}

该URL结合可能会更加复杂,但是这是基本的想法。
对于POST和PUT方法,您还需要复制的请求主体。

The URL combining could be more sophisticated, but that's the basic idea. For the Post and Put methods, you'll also need to copy the request body.

这篇关于如何设置Web API为路由代理控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆