Skip to content

Instantly share code, notes, and snippets.

@jiimaho
Last active December 15, 2019 18:42
Show Gist options
  • Save jiimaho/d454c9f82eecbd309bd1c07443caa6aa to your computer and use it in GitHub Desktop.
Save jiimaho/d454c9f82eecbd309bd1c07443caa6aa to your computer and use it in GitHub Desktop.
Pseudocode example: relation between UseDefaultFiles, UseSpaStaticFiles and UseSpa
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "wwwroot";
});
// ...
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app)
{
// ...
app.UseSwagger();
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1"); });
app.UseMiddleware<HostLoggingMiddleware>();
app.UseMiddleware<ContentSecurityPolicyMiddleware>();
// Re-write path only. / to /index.html
app.UseDefaultFiles();
// Start serve files from wwwroot (Must be after UseDefaultFiles obviously.)
app.UseSpaStaticFiles();
app.UseRouting();
// Your normal routes
app.UseEndpoints(
endpoints =>
{
endpoints.MapDefaultControllerRoute();
endpoints.MapHealthChecks("/hc");
});
// Placed after normal routes, at end of pipeline,
// i.e. if request path did not match static content
// nor other routes, fallback to SPA
app.UseSpa(spa =>
{
if (_environment.IsDevelopment())
{
spa.UseProxyToSpaDevelopmentServer("http://localhost:4000");
}
});
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment