Asp.net core 2.0 website, cache-control not workin

2019-02-20 21:17发布

问题:

In my asp.net core 2.0 application, I am trying to add expiration header (cache-control) into response header for all static resources but it's not adding it all. Below is my code

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddMvc().AddJsonOptions(jsonOptions =>
    {
        jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
        // handle loops correctly
        jsonOptions.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
    });
    services.Configure<CookiePolicyOptions>(options =>
    {
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.None;
    });
    // services.AddResponseCaching();
    services.AddSingleton<Microsoft.Extensions.Configuration.IConfiguration>(Configuration);
    services.AddSingleton<Microsoft.Extensions.Configuration.IConfiguration>(Configuration);
    services.AddSingleton<IMapper>(sp => AutoMapperConfiguration.CreateMapper());

    var builder = new ContainerBuilder();
    builder.Populate(services);
    ApiFactory.RegisterDependencies(builder);
    this.ApplicationContainer = builder.Build();
    return new AutofacServiceProvider(this.ApplicationContainer);
}

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.UseCookiePolicy();       

    app.UseMvc(routes =>
    {
        routes
            .MapRoute(
            name: "goto_sitemap",
            template: "sitemap.xml",
            defaults: new { controller = "Home", action = "Sitemap" });
        routes.MapRoute(name: "default", template: "{controller=Home}/{action=Index}/{id?}");
    });
}

Edit: I removed custom header and added into middleware

public static class BuilderExtensions
{
    public static IApplicationBuilder UseMyMiddleware(this IApplicationBuilder app)
    {
        return app.UseMiddleware<MyMiddleware>();
    }
}

public class MyMiddleware
{
    RequestDelegate _next;

    public MyMiddleware(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {

        if (context.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
        {
            await _next(context);
        }
        else
        {
            Regex rx = new Regex(@"\b(?:https?:\/\/)?[^\/:]+\/.*?\/explore|\/rent|\/about|\/blog|\/partners|\/partner|\/property|\/partnerListing|\/areaguide|\/rentalinquiry|\/termcondition|\/sitemap|\/privacypolicy|\/advertise|\/expired|\/contactus|favicon|\/images|\/api|\/fonts|\/templates|\/css|\/lib|\/js|\/sitemap.xml|\/ror.xml|\/sitemap_mobile.xml",
            RegexOptions.Compiled | RegexOptions.IgnoreCase);
            if (context.Request.Path.Value == "/")
            {
                await _next(context);

            }
            else if (rx.IsMatch(context.Request.Path.Value))
            {
                await _next(context);
                if (context.Request.Path.Value.EndsWith(".css") || context.Request.Path.Value.EndsWith(".js")
                        || context.Request.Path.Value.EndsWith(".jpg") || context.Request.Path.Value.EndsWith(".jpeg")
                        || context.Request.Path.Value.EndsWith(".gif") || context.Request.Path.Value.EndsWith(".png"))
                {

                    //Set css and js files to be cached for 7 days
                    TimeSpan maxAge = new TimeSpan(7, 0, 0, 0);     //7 days
                    context.Response.Headers.Append("Cache-Control", "public,max-age=" + maxAge.TotalSeconds.ToString("0"));

                }
            }
            else
            {
                string path = "/explore" + context.Request.Path.Value;
                context.Request.Path = new PathString(path);
                await _next(context);
            }
        }
    }
}

It was not working, so I added the same in web.config file also, which is also not working.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <staticContent>
  <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="2.00:00:00" cacheControlCustom="public" />
  </staticContent>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" arguments=".\xxxx.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout">
        <environmentVariables> 
            <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" /> 
        </environmentVariables>
    </aspNetCore>
    <httpCompression>
        <dynamicTypes>
            <remove mimeType="text/event-stream" />
            <remove mimeType="*/*" />
            <add mimeType="*/*" enabled="true" />
            <add mimeType="text/event-stream" enabled="true" />
            <add mimeType="image/jpeg" enabled="false" />
            <add mimeType="font/woff2" enabled="false" />
            <add mimeType="image/png" enabled="false" />
            <add mimeType="image/gif" enabled="false" />
        </dynamicTypes>
        <staticTypes>
            <remove mimeType="image/svg+xml" />
            <add mimeType="image/jpeg" enabled="false" />
            <add mimeType="image/svg+xml" enabled="false" />
            <add mimeType="font/woff2" enabled="false" />
            <add mimeType="image/png" enabled="false" />
            <add mimeType="image/gif" enabled="false" />
        </staticTypes>
    </httpCompression>
  </system.webServer>
</configuration>

it stopped working 2nd time onwards (checking with hard refresh)