内联代码导致 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.Clear(); 放在第 500 页的 Page_Load() 中。