YARP matches routes with specified request patterns and forwards them to their destination based on their clusters.
YARP can load proxy configuration from App settings.
// appsettings.json{"ReverseProxy": {"Routes": {"webAppLegacyServePath": {"ClusterId": "webAppLegacyClusterId","Match": {"Path": "/legacyWebapp/{**catch-all}"}}},"Clusters": {"webAppLegacyClusterId": {"Destinations": {"webAppLegacyServePath": {"Address": "http://localhost:5001"}}}}}}
var builder = WebApplication.CreateBuilder(args);builder.Services.AddReverseProxy().LoadFromConfig(builder.Configuration.GetSection("ReverseProxy"));
var app = builder.Build();app.MapReverseProxy();app.Run();
We can configure YARP using a code-based approach. It's suggested to load the proxy configuration by using IProxyConfigProvider in your code. This is handy when you need a flexible proxy setup that matches your application's unique requirements.
var webRoutes = new List<RouteConfig>{// Route for Legacy WebAppnew(){RouteId = "webAppLegacyServePath",ClusterId = webAppLegacyClusterId,Match = new RouteMatch{Path = "/legacyWebapp/{**catch-all}",},},};var webClusters = new List<ClusterConfig>{// Cluster for Legacy WebAppnew(){ClusterId = webAppLegacyClusterId,Destinations = new Dictionary<string, DestinationConfig>{{"webAppLegacyServePath", new DestinationConfig{ Address = webAppLegacyAddress } }}},};
services.AddReverseProxy().Services.AddSingleton<IProxyConfigProvider>(new YarpInMemoryConfiguration(webRoutes, webClusters));// YarpInMemoryConfiguration is the boilerplate class, see the repo for more details.
Check out the Yarp Sample Solution to learn more about how it works and Yarp Solution for side-by-side increment migration.