0

I hosted a website over IIS 10 and added below section config file for cache for static contents.

<configuration>
  <system.webServer>
    <staticContent>
  <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" cacheControlCustom="public" />
  </staticContent>...

and below is startup.cs file section

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    var options = new RewriteOptions()
   .AddRedirect("rent/(.*)", "/$1")
   .AddRedirect("explore/(.*)", "/$1"); 
    app.UseRewriter(options);
    app.UseMyMiddleware();

    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
    app.UseStaticFiles();
    app.UseStaticFiles(new StaticFileOptions
    {
        OnPrepareResponse = ctx =>
        {
            const int durationInSeconds = 60 * 60 * 7;
            ctx.Context.Response.Headers[HeaderNames.CacheControl] =
                "public,max-age=" + durationInSeconds;
        }
    });
}

However it's not adding cache-control in response header for any static resource like images, js, css files.

Can anyone help me out? If some specific information is needed, please let me know, i'll update in question.

3
  • found the issue myself. I used app.UseStaticFiles();, empty one. Commented Mar 4, 2019 at 9:22
  • Please post that as an answer, so this question will move out of the unanswered queue. Commented Mar 4, 2019 at 13:58
  • okay @ChrisPratt Commented Mar 5, 2019 at 13:10

1 Answer 1

1

I was calling UseStaticFiles function twice. Commenting default one fixed the problem.

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    var options = new RewriteOptions()
   .AddRedirect("rent/(.*)", "/$1")
   .AddRedirect("explore/(.*)", "/$1"); 
    app.UseRewriter(options);
    app.UseMyMiddleware();

    loggerFactory.AddConsole(Configuration.GetSection("Logging"));
    loggerFactory.AddDebug();

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseBrowserLink();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }
    //app.UseStaticFiles(); // commenting this fixed the issue.
    app.UseStaticFiles(new StaticFileOptions
    {
        OnPrepareResponse = ctx =>
        {
            const int durationInSeconds = 60 * 60 * 7;
            ctx.Context.Response.Headers[HeaderNames.CacheControl] =
                "public,max-age=" + durationInSeconds;
        }
    });
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.