|
需求:ASP.NET Core 在中间件修改响应头 Server 信息,报错:System.InvalidOperationException: Headers are read-only, response has already started.
错误写法:
报错如下:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HN519819L24M", Request id "0HN519819L24M:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: Headers are read-only, response has already started.
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpHeaders.ThrowHeadersReadOnlyException()
at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http.HttpResponseHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_Server(StringValues value)
异常原因是因为当 ASP.NET Core 已经开始响应了,Headers 响应头则不允许再次被修改了,也就是当开始响应后 Headers 将变成只读状态。
解决方案如下:
如下图:
|
上一篇:Nginx 基于 QUIC 启用 HTTP/3 协议下一篇:使用 dotnet-ef 工具生成回滚脚本
|