【问题标题】:inline code causes ASP.NET to insert 500 page inside the current page内联代码导致 ASP.NET 在当前页面内插入 500 页
【发布时间】:2017-03-31 18:30:18
【问题描述】:

我在 .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,但我尝试对此进行评论,但没有修复它

【问题讨论】:

  • 这不是正常行为 - 整个页面将在 any 响应发送到客户端之前执行。所以它不能向您发送部分响应然后发送不同的响应。您是否通过 AJAX 加载其他页面?好像您的问题缺少MCVE

标签: asp.net webforms custom-errors


【解决方案1】:

我仍然不知道为什么会发生这种情况,但修复起来相当简单。

Response.Clear();放在500页面的Page_Load()中。

【讨论】:

  • 我认为对于内联代码,响应已经在使用页面或控件在点击内联代码之前生成的任何 HTML 构建;因此,当调用自定义错误页面时,它只是添加到现有响应中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-13
相关资源
最近更新 更多