简体   繁体   English

WebApi自定义路由,用于所有流量

[英]WebApi custom route for all traffic

I've been looking for an answer to this question without any luck so maybe someone here has a bit more insight: 我一直在寻找一个没有任何运气的答案,所以也许有人在这里有更多的见识:

1) I have an application that makes http calls. 1)我有一个可以进行http调用的应用程序。 (On box 1) (在方框1上)

2) I have services that access the database and so on. 2)我有访问数据库等的服务。 (On box 2) (在方框2上)

3) I'm working on services that will live in another location and its main purpose to catch all service requests from box 1 and remake the service call from box 2, then return the result to box 1. (A middle man which lives on box 3). 3)我正在开发将位于另一个位置的服务,其主要目的是捕获框1的所有服务请求,并从框2重新进行服务调用,然后将结果返回到框1。方框3)。

Box 1 makes http calls to box 3 which makes calls to box 2, box 3 then returns the result to box 1. 框1向框3发出http调用,框3向框2发出调用,然后框3将结果返回到框1。

I have the code setup to intercept the requests using ExecuteAsync . 我有代码设置使用ExecuteAsync拦截请求。 The issue I'm having is, within the appservice (box 3)- I'm unable to intercept the calls without stubbing out the request functions / routes that exist on box 2 (404 is returned if I don't as the route doesn't exist on box 3 yet). 我遇到的问题是在appservice中(框3)-无法在不存入框2中存在的请求函数/路由的情况下拦截呼叫(如果我不这样做,则返回404在框3上还不存在)。

My ultimate question is: is it possible to allow all requests to pass through the webservice and hit the ExecuteAsync function without the routes / functions ever being defined? 我的最终问题是:是否可以允许所有请求通过Web服务传递并执行ExecuteAsync函数,而无需定义路由/函数?

I've tried multiple variations to the RegisterRoutes function in the RouteConfig and nothing appears to work. 我已经尝试了RouteConfig中RegisterRoutes函数的多种变体,但似乎没有任何效果。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "*",
        defaults: new { controller = "BaseController", action = "Index", id = UrlParameter.Optional }
    );
}

Okay, I figured it out. 好吧,我知道了。

I actually only needed to stub out 1 http request that looks like: 我实际上只需要存根1个看起来像这样的http请求:

[Route("api/{*url}")]
[HttpGet]
public IHttpActionResult Get() {
    return BadRequest();
}

This function never actually gets hit unless the ExecuteAsync function fails. 除非ExecuteAsync函数失败,否则该函数实际上不会被命中。 The ExecuteAsync lives in the same controller and looks like this: ExecuteAsync位于同一控制器中,如下所示:

public override Task<HttpResponseMessage> ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)
    {
        if (Properties.Settings.Default.Redirect)
        {
            var url = controllerContext.Request.RequestUri;
            url = new Uri(url.AbsoluteUri.Replace(
              Properties.Settings.Default.OriginalUriFragment,
              Properties.Settings.Default.ReplacemenUriFragment));
            var client = new HttpClient();
            client.DefaultRequestHeaders.Clear();
            foreach (var httpRequestHeader in controllerContext.Request.Headers)
            {
                client.DefaultRequestHeaders.Add(httpRequestHeader.Key, httpRequestHeader.Value);
            }
            if (controllerContext.Request.Method == HttpMethod.Get)
            {
                return client.GetAsync(url, cancellationToken);
            }
            if (controllerContext.Request.Method == HttpMethod.Post)
            {
                return client.PostAsync(url, controllerContext.Request.Content, cancellationToken);
            }
            if (controllerContext.Request.Method == HttpMethod.Delete)
            {
                return client.DeleteAsync(url, cancellationToken);
            }
            if (controllerContext.Request.Method == HttpMethod.Put)
            {
                return client.PutAsync(url, controllerContext.Request.Content, cancellationToken);
            }
        }

        return base.ExecuteAsync(controllerContext, cancellationToken);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM