Code samples for "Hosting SPA + .NET API solution" article.
Example of WebAPI projects (ASP.NET) servicing a SPA front-end along with the API and Swagger/OpenAPI interface, and managing cache settings of the front-end assets.
Route | Handler |
---|---|
Root (e.g. / ) or /index.html |
Returns index.html with specified caching headers |
Any static file, e.g. styles.css |
Returns the requested static file with specified caching headers |
Any unknown route, e.g. a SPA page or unknown page/resource | Returns index.html to let the SPA to show the "404 Not Found" |
/swagger/* |
The Swagger/OpenAPI interface |
API end-points, e.g. /api/* |
Processed by the API controllers/routes |
The key elements of the solution:
wwwroot
– a sample SPA front-end with two routes serviced by the back-end project.ClassicApi
– a WebAPI project with classic controllers servicing the front-end fromwwwroot
.MinimalApi
– a WebAPI project built with minimal API servicing the front-end fromwwwroot
.Configuration
– helper methods used by both projects to configure the middleware for Swagger and SPA.