user8900983
user8900983

Reputation:

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. (On box 1)

2) I have services that access the database and so on. (On box 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).

Box 1 makes http calls to box 3 which makes calls to box 2, box 3 then returns the result to box 1.

I have the code setup to intercept the requests using 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).

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?

I've tried multiple variations to the RegisterRoutes function in the RouteConfig and nothing appears to work.

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 }
    );
}

Upvotes: 0

Views: 81

Answers (1)

Eddy Howard
Eddy Howard

Reputation: 127

Okay, I figured it out.

I actually only needed to stub out 1 http request that looks like:

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

This function never actually gets hit unless the ExecuteAsync function fails. The ExecuteAsync lives in the same controller and looks like this:

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);
    }

Upvotes: 0

Related Questions