2017-08-06 31 views
0

所以我按照指南here(不包括Application Insights等一些特定于应用程序的项目)。ASP.NET Core 1.1中的浏览器缓存没有触发

Startup.cs修改在这两个方面:

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddResponseCompression(options => 
    { 
    options.Providers.Add<GzipCompressionProvider>(); 
    options.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(new[] { "image/svg+xml" }); 
    }); 

    // Add framework services. 
    services.AddDbContext<ApplicationDbContext>(options => 
     options.UseSqlServer(Configuration.GetConnectionString("DatabaseConnection"))); 

    services.AddDbContext<DatabaseContext>(options => 
    options.UseSqlServer(Configuration.GetConnectionString("DatabaseConnection"))); 


    services.AddIdentity<ApplicationUser, IdentityRole>() 
     .AddEntityFrameworkStores<ApplicationDbContext>() 
     .AddDefaultTokenProviders(); 
    services.AddResponseCaching(); 
    services.AddMvc(); 

    // Add application services. 
    services.AddTransient<IEmailSender, AuthMessageSender>(); 
    services.AddTransient<ISmsSender, AuthMessageSender>(); 
} 

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 
{ 
    app.UseResponseCompression(); 
    app.UseResponseCaching(); 
    loggerFactory.AddConsole(Configuration.GetSection("Logging")); 
    loggerFactory.AddDebug(); 

    if (env.IsDevelopment()) 
    { 
    app.UseDeveloperExceptionPage(); 
    app.UseDatabaseErrorPage(); 
    app.UseBrowserLink(); 
    } 
    else 
    { 
    app.UseExceptionHandler("/Home/Error"); 
    } 

    app.UseStaticFiles(); 

    app.UseIdentity(); 

    // Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715 

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

和各种控制器的网页,我打电话ResponseCache中像这样(将其设置为2分钟,不知道这是什么人traditionallly设置到):

[ResponseCache(Duration = 120)] 
public IActionResult Index() 
{ 
    return View(); 
} 

在部署到Azure之后,我可以重复击中F5并仍然可以看到200个网络流量。目标是缓存CSS,JS和页面(以及不属于[Authorize]管理控制器的页面)。

更新(各种):不幸的是,我已经审查了很多指南,似乎他们都固有地说同样的事情(主要区别是不依赖于响应缓存Nuget包,并与一个硬编码版本,虽然相同的非结果),但由于某种原因,这是行不通的。在Web Forms应用程序(2.0升级到4.0)中,我能够以最小的努力实现这一目标。

回答

0

当你点击F5时,你将总是得到一个新的回应,绕过缓存。如果您尝试导航到链接(...),您会看到响应确实被缓存。

+0

通常,当我只碰到F5时,我在那里获得304s以及其他已完成的网站。这一个是所有的200。现在,我可以做一个很难绕过缓存的地方,但这不是这种情况。使我注意到这一点的主要症状是PageSpeed Insights标记图像/ css /等。因为没有缓存标题 – Robert

+0

要进行测试,请将A元素添加到正在缓存的页面(或操作),然后尝试导航到该页面(或操作)。你会注意到这个动作没有被击中。 –

+0

从索引开始,我点击了开发工具中Network标签的页面内的链接。所有的资产都回来了200个。即使是构成主题的布局和CSS的一部分的图像(也可以从布局中获得)。 – Robert