Page_Init 和 Page_Load 只在 IE 中执行两次 ASP & VB.NET .. 为什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20471098/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 16:04:52  来源:igfitidea点击:

Page_Init and Page_Load executes twice ASP & VB.NET only in IE .. why?

asp.netvb.netpostbackhttpresponsepageload

提问by user2965112

Having a strange problem today. I have managed to get my other problems fixed. My ASP page contains NO controls (so there are no blank image url's that could cause the problem). The program itself, from the VB code behind, gets binary data from a database and does a Response.BinaryWrite(imagedata) to the page. When I run it in Chrome or Firefox, everything works perfectly. When I run in IE, after my code finishes execution, it runs Page_Init again and Page_Load again as if loading for the very first time all over again. The value of postback is always false so there is no way around it. Here is the asp code (as you can see, nothing of interest)...

今天遇到一个奇怪的问题。我已经设法解决了我的其他问题。我的 ASP 页面不包含任何控件(因此没有可能导致问题的空白图像 url)。程序本身,从后面的 VB 代码中,从数据库中获取二进制数据并对页面执行 Response.BinaryWrite(imagedata)。当我在 Chrome 或 Firefox 中运行它时,一切正常。当我在 IE 中运行时,在我的代码完成执行后,它再次运行 Page_Init 和 Page_Load,就好像再次加载第一次一样。postback 的值总是假的,所以没有办法绕过它。这是asp代码(如您所见,没什么兴趣)...

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="pagenamehere.aspx.vb" Inherits="x.x.x.x.y" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Test</title>
</head>

<body>
</body>
</html>

And the Page_Init and Page_Load signatures are as follows

并且 Page_Init 和 Page_Load 签名如下

Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
'checks database connection string and handles error if there is one
End Sub

Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
'checks user is authenticated to view the image and if so, does the binarywrite
End Sub

I have tried change autoeventwriteup to true and taking off the handles Me.Load etc but with no effect. I've also tried checking the Sender object but nothing changes between the first time the page loads and the unwanted second load of the page.

我曾尝试将 autoeventwriteup 更改为 true 并取消处理 Me.Load 等,但没有任何效果。我也试过检查 Sender 对象,但在页面第一次加载和页面不需要的第二次加载之间没有任何变化。

Lastly, the page loads for the second time straight after the following code

最后,页面在以下代码之后第二次加载

                Response.Clear()
                Response.ClearContent()
                Response.ClearHeaders()
                Response.Buffer = True
                Response.ContentType = "image/vnd.djvu"
                Response.AddHeader("Content-Disposition", "inline;filename=temp.djvu")
                Response.Cache.SetCacheability(HttpCacheability.NoCache)
                Response.BinaryWrite(imagedata)
                Response.Flush()
                Response.Close()

Straight after Response.Close it will go back to Page_Init. I have tried response.end but it makes no difference. I have tried removing the header, the cache information etc but no luck. Please help!

在 Response.Close 之后,它将立即返回 Page_Init。我试过 response.end 但它没有区别。我曾尝试删除标题、缓存信息等,但没有成功。请帮忙!

Thanks

谢谢

回答by Josh Darnell

You really shouldn't use Response.Closelike that*. It's really just for protecting against attacks or other error-type conditions:

你真的不应该那样使用Response.Close*。它实际上只是为了防止攻击或其他错误类型的条件

This method terminates the connection to the client in an abrupt manner and is not intended for normal HTTP request processing. The method sends a reset packet to the client, which can cause response data that is buffered on the server, the client, or somewhere in between to be dropped.

You might use this method in response to an attack by a malicious HTTP client. However, typically you should call CompleteRequest instead if you want to jump ahead to the EndRequest event and send a response to the client.

此方法以突然的方式终止与客户端的连接,不适用于正常的 HTTP 请求处理。该方法向客户端发送一个重置数据包,这会导致缓存在服务器、客户端或两者之间的响应数据被丢弃。

您可以使用此方法来响应恶意 HTTP 客户端的攻击。但是,如果您想跳转到 EndRequest 事件并向客户端发送响应,通常应该调用 CompleteRequest。

And it will cause very similar problems to the one you're describing (because it sends a "reset" request to the client). Try updating your code to use Response.CompleteRequestinstead, and this should (probably) resolve your "multiple request" issues.

它会导致与您描述的问题非常相似的问题(因为它会向客户端发送“重置”请求)。尝试更新您的代码以改用Response.CompleteRequest,这应该(可能)解决您的“多个请求”问题。

*See this blog post for an in-depth analysis of why: Response.End, Response.Close, and How Customer Feedback Helps Us Improve MSDN Documentation

*有关原因的深入分析,请参阅此博客文章:Response.End、Response.Close 以及客户反馈如何帮助我们改进 MSDN 文档