Skip to content
ASP.NET Core Proxies made easy.
Branch: master
Clone or download
Latest commit 5d5e56d May 7, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Update main.workflow Mar 16, 2019
src Update version. Apr 22, 2019
.gitignore First commit. Oct 5, 2017
.gitpod.yml Add badges and gitpod. May 7, 2019
.travis.yml Travis fix. Oct 24, 2017
AspNetCore.Proxy.sln Move some shit around and add tests. Oct 17, 2017
LICENSE First commit. Oct 5, 2017
README.md Add badges and gitpod. May 7, 2019

README.md

GitHub Actions Status GitHub Release NuGet Version NuGet Downloads

AspNetCore.Proxy

ASP.NET Core Proxies made easy.

Information

Install

dotnet add package AspNetCore.Proxy

Test

Download the source and run.

dotnet restore
dotnet test src/Test/AspNetCore.Proxy.Tests.csproj

Compatibility

Latest .NET Standard 2.0.

Examples

First, you must add the required services.

public void ConfigureServices(IServiceCollection services)
{
    ...
    services.AddProxies();
    ...
}

Existing Controller

You can use the proxy functionality on an existing Controller by leveraging the Proxy extension method.

public class MyController : Controller
{
    [Route("api/posts/{postId}")]
    public Task GetPosts(int postId)
    {
        return this.ProxyAsync($"https://jsonplaceholder.typicode.com/posts/{postId}");
    }
}

Application Builder

You can define a proxy in Configure(IApplicationBuilder app, IHostingEnvironment env). The arguments are passed to the underlying lambda as a Dictionary.

app.UseProxy("api/{arg1}/{arg2}", async (args) => {
    // Get the proxied address.
    return await SomeCallThatComputesAUrl(args["arg1"], args["arg2"]);
});

ProxyRoute Attribute

You can also make the proxy look and feel almost like a route, but as part of a static method.

First, add the middleware.

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    ...
    app.UseProxies();
    ...
}

Then, create a static method which returns a Task<string> or string (where the string is the URI to proxy).

[ProxyRoute("api/posts/{arg1}/{arg2}")]
public static async Task<string> GetProxy(string arg1, string arg2)
{
    var uri = await SomeCallThatComputesAUrl(arg1, arg2);
    return uri;
}

[ProxyRoute("api/comments/{postId}")]
public static async Task<string> GetComments(int postId) => $"https://jsonplaceholder.typicode.com/posts/{postId}";

License

The MIT License (MIT)

Copyright (c) 2017 Aaron Roney

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
You can’t perform that action at this time.