Reputation: 6434
I am trying figure out how to implement GetVaryByCustomString function for asp.net core 1.0.
Have you implemented that kind of functionality for asp.net core 1.0?
Thanks
Upvotes: 2
Views: 567
Reputation: 6434
After I ask this question, using Middleware has suddenly came to my mind and i have implemented a class like below:
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);
}
}
and then, there is the extension method for it:
public static class OutputCacheHeaderExtensions
{
public static IApplicationBuilder UseOutputCacheHeader(this IApplicationBuilder builder)
{
return builder.UseMiddleware<OutputCacheHeaderMiddleware>();
}
}
and in Startup.cs Configure method, i added 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?}");
});
}
and on Controller:
[ResponseCache(VaryByHeader = "dt-cache-user", Duration = 6000)]
public IActionResult Index()
{
return View();
}
After all of this, when i debug it, i can see that there is a header "dt-cache-user" with the proper value but ResponseCache isn't working. Everytime I hit F5 to refresh the page it always hit debug point.
What might be the reason that it doesn't work?
Thanks.
Upvotes: 1