In ASP.NET Core static files(images, css,…) are typically served using the Static file middleware. The static file middleware can be configured by adding a dependency on the Microsoft.AspNetCore.StaticFiles package to your project and then calling the UseStaticFiles
extension method from Startup.Configure
:
Unfortunately this code will not do its job in the most efficient way. By default, no caching is applied meaning that the browser will request these files again and again increasing the load on your server.
Luckily it’s not that hard to change the middleware configuration to introduce caching. In this example we set the caching to one day:
Remark: An alternative approach would be to let your proxy server(IIS,…) handle the static file requests as discussed here.