asp.net-corems-yarp

Send multiple routes to the same destination address


Can I match multiple paths in yarp?

E.g. I have this:

endpoints.Map("/place/{**catch-all}", async httpContext =>
{
    var error = await forwarder.SendAsync(httpContext, "https://maps.googleapis.com/maps/api/",
        httpClient, requestConfig, transformer);
    // Check if the operation was successful
    if (error != ForwarderError.None)
    {
        var errorFeature = httpContext.GetForwarderErrorFeature();
        var exception = errorFeature.Exception;
    }
});

But I would like to do the same for "/geocode/{**catch-all}", without code duplication.

Because the route is just http://localhost/geocode... or http://localhost/place...

So there is nothing before it to match on. I can add something before it instead like http://localhost/proxyme/place/... in my front end, but then in my CustomTransformer, httpContext.Request.Path has "proxyme/" in it and I don't know how to remove it for use here: proxyRequest.RequestUri = RequestUtilities.MakeDestinationAddress("https://maps.googleapis.com/maps/api", httpContext.Request.Path, queryContext.QueryString); without just doing primitive string operations to delete "proxyme/". So that could be the other option if there is an elegant way to delete "proxyme/" from the httpContext.Request.Path.


Solution

  • Just reuse the same method in the 2 routes:

    RequestDelegate handler = async httpContext =>
    {
        var error = await forwarder.SendAsync(httpContext, "https://maps.googleapis.com/maps/api/",
            httpClient, requestConfig, transformer);
        // Check if the operation was successful
        if (error != ForwarderError.None)
        {
            var errorFeature = httpContext.GetForwarderErrorFeature();
            var exception = errorFeature.Exception;
        }
    }
    
    endpoints.Map("/place/{**catch-all}", handler);
    endpoints.Map("/geocode/{**catch-all}", handler);