Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

首次访问Status显示:(failed) net::ERR_HTTP2_PROTOCOL_ERROR, 页面也没有正常展示 #2

Open
dean-shanghai opened this issue Sep 14, 2024 · 4 comments

Comments

@dean-shanghai
Copy link

首次访问Status显示:(failed) net::ERR_HTTP2_PROTOCOL_ERROR, 页面也没有正常展示,如图:

首次访问异常

@toolgood
Copy link
Owner

ERR_HTTP2_PROTOCOL_ERROR 是一个Chrome浏览器中的错误,表明浏览器在使用HTTP/2协议时遇到了协议违规的情况。这可能是由于服务器发送了不符合HTTP/2标准的响应导致的。

@toolgood
Copy link
Owner

我更新到.net 8,没有重现这个问题

@dean-shanghai
Copy link
Author

dean-shanghai commented Sep 16, 2024

报错情况只在首次访问页面时出现(首次访问页面:html文件夹中还没有生成该页面的.html/.gzip/.br等缓存文件)。

报错信息:System.InvalidOperationException: Headers are read-only, response has already started.

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
An unhandled exception has occurred while executing the request.
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.HttpHeaders.Microsoft.AspNetCore.Http.IHeaderDictionary.set_Item(String key, StringValues value)
at Microsoft.AspNetCore.Mvc.HtmlStaticFileAttribute.OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next) in C:...\Attributes\HtmlStaticFileAttribute.cs:line

报错的位置是这一句:context.HttpContext.Response.Headers["Cache-Control"] = "max-age=" + (CurrPageExpireMinutes ?? ExpireMinutes) * 60;

@toolgood
Copy link
Owner

修改好了,添加了一个判断

                if (context.HttpContext.Response.Headers.IsReadOnly == false) {
                    var fi = new FileInfo(filePath);
                    var etag = fi.LastWriteTimeUtc.Ticks.ToString();
                    context.HttpContext.Response.Headers["Cache-Control"] = "max-age=" + (CurrPageExpireMinutes ?? ExpireMinutes) * 60;
                    context.HttpContext.Response.Headers["Etag"] = etag;
                    context.HttpContext.Response.Headers["Date"] = DateTime.Now.ToString("r");
                    context.HttpContext.Response.Headers["Expires"] = DateTime.Now.AddMinutes(CurrPageExpireMinutes ?? ExpireMinutes).ToString("r");
                }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants