10

I am migrating code from an existing WebApi 2 project and I am wondering how to perform the equivalent of the code below in ASP.NET 5 MVC 6. I don't see any route code which accepts a handler option.

config.Routes.MapHttpRoute("SomeApiProxy", "api/someapi/{*path}",
    handler: HttpClientFactory.CreatePipeline(new HttpClientHandler(), new DelegatingHandler[] {new ForwardingProxyHandler(new Uri("http://some-api.com/api/v2/"))}),
    defaults: new {path = RouteParameter.Optional},
    constraints: null
);
1
  • Is it just get requests or any request? Commented Jan 21, 2016 at 22:06

2 Answers 2

11

This is just off the top of my head but you could create a middleware. This would work for get requests with no headers but could be modified to do more.

app.Use( async ( context, next ) =>
{
    var pathAndQuery = context.Request.GetUri().PathAndQuery;

    const string apiEndpoint = "/api/someapi/";
    if ( !pathAndQuery.StartsWith( apiEndpoint ) )
        //continues through the rest of the pipeline
        await next();
    else
    {
        using ( var httpClient = new HttpClient() )
        {
            var response = await httpClient.GetAsync( "http://some-api.com/api/v2/" + pathAndQuery.Replace( apiEndpoint, "" ) );
            var result = await response.Content.ReadAsStringAsync();

            context.Response.StatusCode = (int)response.StatusCode;
            await context.Response.WriteAsync( result );
        }
    }
} );

If you put this befire app.UseMvc() it will intercept any requests where the path starts with /api/someapi

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your reply, I was able to expand on this and create a full middleware that does what I needed.
No problem am glad I could help
I tried to build that into something reusable for everyone. You can find it on GitHub here: github.com/ZoolWay/GlacierCrates.AspNetCore.ApiProxy
I have an internally hosted API but would like 3rd party to use it from outside the network? How would it work for my purpose? stackoverflow.com/questions/43002283/…
3

We actually wrote a middleware for this (well our intern did :)) https://github.com/aspnet/Proxy

3 Comments

Thanks. It looks like it proxies all requests though. Am I missing something? I need to proxy certain requests based on the route/URL/request that is incoming. Either way, it is a helpful reference.
Cool, thanks for sharing! Just wanted to ask - does it have sockets problem described here (as creating new HttpClient()): aspnetmonsters.com/2016/08/2016-08-27-httpclientwrong
@ChrisPutnam . Use app.UseWhen to pick and choose where/when you want it applied. Proxy middleware worked great. Thumbs up to the intern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.