Skip to content

Commit 824a62d

Browse files
committed
feat: Use filter to add header "Cache-Control" into response instead of middleware
1 parent 648477d commit 824a62d

3 files changed

Lines changed: 22 additions & 31 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Microsoft.AspNetCore.Mvc.Filters;
2+
3+
namespace ReferenceProject.Filters
4+
{
5+
public class CacheControlFilter: IResultFilter
6+
{
7+
public void OnResultExecuted(ResultExecutedContext context)
8+
{
9+
10+
}
11+
12+
public void OnResultExecuting(ResultExecutingContext context)
13+
{
14+
if( context.HttpContext.Request.Method == "GET" )
15+
{
16+
context.HttpContext.Response.Headers.Add(
17+
"Cache-Control", new string[] { "no-cache, no-store, must-revalidate" });
18+
}
19+
}
20+
}
21+
}

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Middleware/PreventResponseCachingMiddleware.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

ProjectTemplates/AspNetCore.WebApi/ReferenceProject/ReferenceProject/Startup.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public void ConfigureServices(IServiceCollection services)
6666
.AddMvc(options =>
6767
{
6868
options.Filters.Add(new ValidateModelFilter());
69+
options.Filters.Add(new CacheControlFilter());
6970
})
7071
.AddJsonOptions(options =>
7172
{
@@ -112,8 +113,6 @@ public void Configure(IApplicationBuilder app/*, IHostingEnvironment env*/)
112113
// Use our exception handler middleware before any other handlers
113114
app.UseExceptionHandler();
114115

115-
app.UseMiddleware<PreventResponseCachingMiddleware>();
116-
117116
app.UseCors(builder => builder
118117
.AllowAnyOrigin()
119118
.AllowAnyMethod()

0 commit comments

Comments
 (0)