我正在尝试如何在asp.net Core1.0中实现GetVaryByCustomString函数。
你在asp.net Core1.0中实现过这样的功能吗?
谢谢
发布于 2016-05-06 21:43:26
在我问完这个问题后,我突然想到了使用中间件,我实现了一个如下所示的类:
public class OutputCacheHeaderMiddleware
{
private readonly RequestDelegate _next;
public OutputCacheHeaderMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext context)
{
var user = UserHelper.GetUser(context);
if (user?.UserInfos != null)
{
var key = "user_1_a_" + string.Join(",", user.UserInfos.Select(u => u.Id));
context.Request.Headers.Add("dt-cache-user", key);
}
await _next.Invoke(context);
}
}然后,还有它的扩展方法:
public static class OutputCacheHeaderExtensions
{
public static IApplicationBuilder UseOutputCacheHeader(this IApplicationBuilder builder)
{
return builder.UseMiddleware<OutputCacheHeaderMiddleware>();
}
}在Startup.cs Configure方法中,我添加了app.UseOutputCacheHeader();
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseOutputCacheHeader();
app.UseIISPlatformHandler();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}在控制器上:
[ResponseCache(VaryByHeader = "dt-cache-user", Duration = 6000)]
public IActionResult Index()
{
return View();
}完成所有这些之后,当我调试它时,我可以看到有一个头文件"dt-cache-user“具有正确的值,但是ResponseCache不工作。每次我点击F5刷新页面时,它总是命中调试点。
它不工作的原因可能是什么?
谢谢。
https://stackoverflow.com/questions/37072796
复制相似问题