StaticFileOptions.OnPrepareResponse does not get called for index.html
我目前正在尝试为具有.NET Core 2.2后端的Angular SPA禁用
我正在根据此答案通过为我的
但是
之外的所有文件中断
我在这里想念什么?如何实际控制
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // I've changed nothing else in the default ASP.NET Core/Angular template public void Configure(IApplicationBuilder app, IHostingEnvironment env) { // ... var staticFileOptions = new StaticFileOptions { OnPrepareResponse = context => { // Breakpoint for next line hits for following files // 1: styles.61d14a95058dbe9da495.css // 2: runtime.a66f828dca56eeb90e02.js // 3: polyfills.7a0e6866a34e280f49e7.js // 4: main.d9791b5a6df420d81994.js // 5: favicon.ico if (context.File.Name =="index.html") { context.Context.Response.Headers .Add("Cache-Control","no-cache, no-store, must-revalidate"); context.Context.Response.Headers.Add("Pragma","no-cache"); context.Context.Response.Headers.Add("Expires","0"); } } }; app.UseStaticFiles(staticFileOptions); app.UseSpaStaticFiles(staticFileOptions); // ... } |
您可以使用此代码查看它是否对您有用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | app.UseSpaStaticFiles(new StaticFileOptions() { OnPrepareResponse = ctx => { var headers = ctx.Context.Response.GetTypedHeaders(); headers.CacheControl = new CacheControlHeaderValue { Public = true, MaxAge = TimeSpan.FromDays(0) }; } }); app.UseSpa(spa => { spa.Options.SourcePath ="ClientApp"; spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions() { OnPrepareResponse = ctx => { var headers = ctx.Context.Response.GetTypedHeaders(); headers.CacheControl = new CacheControlHeaderValue { Public = true, MaxAge = TimeSpan.FromDays(0) }; } }; if (env.IsDevelopment()) { spa.UseAngularCliServer(npmScript:"start"); } }); |
我想出的另一个解决方案是这样的。我不确定我是否喜欢这种解决方案,但至少它能起作用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | app.Use(async (context, next) => { context.Response.OnStarting(() => { var requestPath = context.Request.Path.Value; if (requestPath.EndsWith("index.html")) { context.Response.Headers.Add("Cache-Control","no-cache, no-store, must-revalidate"); context.Response.Headers.Add("Pragma","no-cache"); context.Response.Headers.Add("Expires","0"); } return Task.FromResult(0); }); await next(); }); |