如何将缓存控制设置为无存储,将杂注设置为无缓存?
当前,当我运行我的应用程序时出现此错误:
启动应用程序时发生错误。 InvalidOperationException:无法解析类型的服务 'Microsoft.AspNetCore.ResponseCaching.Internal.IResponseCachingPolicyProvider' 尝试激活时 “ Microsoft.AspNetCore.ResponseCaching.ResponseCachingMiddleware”。
这是我的startup.cs
app.UseResponseCaching();
app.Use(async (context, next) =>
{
context.Response.GetTypedHeaders().CacheControl =
new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
{
NoStore = true,
NoCache = true,
};
await next();
});
我还需要设置什么?
答案 0 :(得分:1)
您已经这样在ConfigureService中注册了吗?
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCaching();
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseResponseCaching();
app.Use(async (context, next) =>
{
context.Response.GetTypedHeaders().CacheControl =
new Microsoft.Net.Http.Headers.CacheControlHeaderValue()
{
Public = true,
MaxAge = TimeSpan.FromSeconds(10)
};
context.Response.Headers[Microsoft.Net.Http.Headers.HeaderNames.Vary] =
new string[] { "Accept-Encoding" };
await next();
});
app.UseMvc();
}
更新后,您可以找到如何设置问题here
的用法