KevinDockx / HttpCacheHeaders

ASP.NET Core middleware that adds HttpCache headers to responses (Cache-Control, Expires, ETag, Last-Modified), and implements cache expiration & validation models
MIT License
271 stars 57 forks source link

Investigate correct placement of middleware for default .NET 6 templates #100

Closed KevinDockx closed 2 years ago

KevinDockx commented 3 years ago

Default templates will probably no longer call into UseRouting / UseEndpoints. Currently, UseCacheHeaders must be placed between these two statements. Investigate whether to / how to update documentation in accordance with default .NET 6 template. Potentially provide a sample.

Cfr:

app.MapControllers(endpoints => { // cache headers here? });

wbuck commented 2 years ago

Hello Kevin. I've run in to this issue using the default dotnet 6 template and I'm curious where I should place UseHttpCacheHeaders( )? Currently I've done the following:

app.UseHttpCacheHeaders( );
app.UseRouting( );
app.UseAuthorization( );
app.MapControllers( );

As you can see I explicitly call UseRouting which I believe is now called in the WebApplicationBuilder. As per MSDN they mention that if we need to we can call those methods which are currently called in the WebApplicationBuilder.

MJomaa commented 2 years ago

@wbuck You are correct, it's now in the builder. But you can call UseRouting() to specify where in the pipeline the route matching will happen.

See: https://docs.microsoft.com/en-us/aspnet/core/migration/50-to-60?view=aspnetcore-6.0&tabs=visual-studio

"When using the minimal hosting model, the endpoint routing middleware wraps the entire middleware pipeline, therefore there's no need to have explicit calls to UseRouting or UseEndpoints to register routes. UseRouting can still be used to specify where route matching happens, but UseRouting doesn't need to be explicitly called if routes should be matched at the beginning of the middleware pipeline."

KevinDockx commented 2 years ago

As mentioned by @MJomaa, you can still call into UseRouting manually. Yet it's not strictly necessary for the cache headers middleware. I added a .NET 6 sample: https://github.com/KevinDockx/HttpCacheHeaders/tree/master/sample/Marvin.Cache.Headers.Sample.NET6 :)