2

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

Kévin Chalet
  • 39,509
  • 7
  • 121
  • 131
Barbaros Alp
  • 6,405
  • 8
  • 47
  • 61

1 Answers1

1

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.

Barbaros Alp
  • 6,405
  • 8
  • 47
  • 61
  • Response cache caching depends on the browser. Google Chrome, for example, will always send a request if you manually refresh the page, or when dev tools is set to disable the cache. Try clicking on a link to your index page after navigating to it once. – Will Ray May 06 '16 at 14:35
  • I especially controlled the "Disable Cahce" checkbox and it was unchecked. Both refresh and navigate actions didn't work. – Barbaros Alp May 06 '16 at 14:42
  • Any progress on this? @BarbarosAlp – ibrahimozgon Nov 07 '18 at 12:14
  • You havent specified the `Location` for the cache in `[ResponseCache]`, so it caches stuff in the browser, not on the server. And hitting F5 usually invalidates browser cache, thats why you're hitting the breakpoint – Alex from Jitbit Jun 12 '20 at 11:11