内联代码导致 ASP.NET 在当前页面内插入 500 页
inline code causes ASP.NET to insert 500 page inside the current page
我在 .ascx 中有一些代码,如下所示:
<%
int a = 0;
int b = 5;
int c = b/a;
%>
如您所料抛出 500 错误(除以 0)。
CustomErrors 看起来像这样
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="500" redirect="/500.aspx" />
</customErrors>
我尝试访问的页面呈现混乱。查看源代码,我看到了正常页面的一半,然后是 500 页。
<div class="mapTextOverlay" style="width:448px;padding-left:232.96px;">
[main page abruptly ends here and the 500 page starts]
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head"><title>
500
</title>
[etc.]
所以服务器传输发生在页面呈现的中间。
如何防止出现 500 错误时呈现部分页面的可能性?
- 这只发生在内联代码中,如果错误在代码隐藏中,则错误会正常显示。
- 代码中没有任何内容调用 Response.Flush()
- Response.BufferOutput 是真的
- 代码正在设置 Response.Filter,但我尝试对其进行评论,但没有修复它
我仍然不知道为什么会这样,但事实证明修复相当简单。
将 Response.Clear();
放在第 500 页的 Page_Load()
中。
我在 .ascx 中有一些代码,如下所示:
<%
int a = 0;
int b = 5;
int c = b/a;
%>
如您所料抛出 500 错误(除以 0)。
CustomErrors 看起来像这样
<customErrors mode="On" redirectMode="ResponseRewrite">
<error statusCode="500" redirect="/500.aspx" />
</customErrors>
我尝试访问的页面呈现混乱。查看源代码,我看到了正常页面的一半,然后是 500 页。
<div class="mapTextOverlay" style="width:448px;padding-left:232.96px;">
[main page abruptly ends here and the 500 page starts]
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head"><title>
500
</title>
[etc.]
所以服务器传输发生在页面呈现的中间。
如何防止出现 500 错误时呈现部分页面的可能性?
- 这只发生在内联代码中,如果错误在代码隐藏中,则错误会正常显示。
- 代码中没有任何内容调用 Response.Flush()
- Response.BufferOutput 是真的
- 代码正在设置 Response.Filter,但我尝试对其进行评论,但没有修复它
我仍然不知道为什么会这样,但事实证明修复相当简单。
将 Response.Clear();
放在第 500 页的 Page_Load()
中。