我尝试做一个简单的appversion检查通过发送当前的服务器版本到客户端在http头,如果有一个较新的版本,它应该通知用户重新加载。我认为这将是一个简单的任务,但我的角拦截器没有得到我的新响应头由asp.net core发送。我使用。net 6和Angular 13。
Startup.cs
app.Use(async (context, next) =>
{
context.Response.Headers.Add("X-AppVersion", Assembly.GetEntryAssembly()!.GetName().Version!.ToString());
await next();
});
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
spa.Options.DefaultPageStaticFileOptions = new StaticFileOptions
{
OnPrepareResponse = context =>
{
var headers = context.Context.Response.GetTypedHeaders();
headers.CacheControl = new CacheControlHeaderValue
{
NoCache = true,
NoStore = true,
MustRevalidate = true,
MaxAge = TimeSpan.Zero
};
// Not sure about this one
headers.Append("X-AppVersion", Assembly.GetEntryAssembly()!.GetName().Version!.ToString());
}
};
});
角拦截
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const reqWithCredentials = req.clone({ withCredentials: true });
return next.handle(reqWithCredentials).pipe(
map(resp => {
if (resp instanceof HttpResponse) {
if (resp.headers.get('x-appversion')) {
console.log(resp.headers.get('x-appversion'));
} else {
console.log(null); // I always get null
}
return resp;
}
})
)
}
我必须移动我的app.Use()中间件配置在之前app.UseEndpoints(),
相关:https://github.com/dotnet/aspnetcore/issues/18768