blog3000/Blog3000/Server/MiddleWares/NoCacheHeaderAdder.cs

42 lines
970 B
C#

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Blog3000.Server.MiddleWares
{
public class NoCacheHeaderAdder
{
private string[] urls;
public NoCacheHeaderAdder(string[] urls)
{
this.urls = urls;
}
public void Process(HttpContext context)
{
var p = context.Request.Path.ToString().ToLowerInvariant();
bool noCache = false;
foreach (var u in urls)
{
if (p.StartsWith(u))
{
noCache = true;
break;
}
}
if (noCache)
{
context.Response.Headers.Add("Pragma", "no-cache"); // HTTP1.0
context.Response.Headers.Add("Cache-Control", "no-cache, must-revalidate"); // HTTP1.1
}
}
}
}