Skip to content

AspNetCore.Proxy 3.0.1

Choose a tag to compare

@twitchax twitchax released this 05 Sep 23:35
· 122 commits to master since this release
9886485

This release allows the developer to add ProxyOptions to the proxied calls. These options instruct the proxier to perform specific actions during parts of the proxy operation, failures, etc.

This release is a breaking change. The handleFailure proxy parameter must now be supplied via the ProxyOptions.

public class MyController : Controller
{
    [Route("api/posts/{postId}")]
    public Task GetPosts(int postId)
    {
        var options = ProxyOptions.Instance
            .WithShouldAddForwardedHeaders(false)
            .WithBeforeSend((c, hrm) =>
            {
                // Set something that is needed for the downstream endpoint.
                hrm.Headers.Authorization = new AuthenticationHeaderValue("Basic");
            })
            .WithAfterReceive((c, hrm) =>
            {
                // Alter the conent in  some way before sending back to client.
                var newContent = new StringContent("It's all greek...er, Latin...to me!");
                hrm.Content = newContent;
            })
            .WithHandleFailure((c, e) =>
            {
                // Return a custom error response.
                c.Response.StatusCode = 403;
                c.Response.WriteAsync("Things borked.");
            });

        return this.ProxyAsync($"https://jsonplaceholder.typicode.com/posts/{postId}");
    }
}