I’m developing a distributed system and have an ASP.NET Core application that should serve a Single Page Application (SPA) to the client. All HTTP requests from this SPA should go back to this ASP.NET Core application but should be proxied to another backend service.
using Microsoft.Extensions.FileProviders;
using Ocelot.DependencyInjection;
using Ocelot.Middleware;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOcelot(builder.Configuration);
builder.Services.AddControllers();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
app.UseOcelot().Wait();
app.UseSpa(spa =>
{
spa.Options.SourcePath = "my-angular-app";
if (builder.Environment.IsDevelopment())
{
spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");
}
else
{
spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(builder.Environment.ContentRootPath, "my-angular-app/dist/my-angular-app/browser"))
};
}
});
app.Run();
And ocelot.json
"Routes": [
{
"DownstreamPathTemplate": "/{everything}",
"DownstreamScheme": "http",
"DownstreamHostAndPorts": [
{
"Host": "localhost",
"Port": 32500
}
],
"UpstreamPathTemplate": "/api/{everything}",
"UpstreamHttpMethod": [ "Get", "Post", "Put", "Delete" ]
}
],
"GlobalConfiguration": {
"BaseUrl": "http://localhost:5000"
}
}
New contributor
Kostia Yam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.