简体   繁体   中英

asp.net Page loading twice when making an underlying connection

I have a aspx page that seems to be loading twice when I enter the Url to the page.

In this page's loading event, I'm making an connection to a server to retrieve a document and then I output the downloaded bytes to the output stream of the page.

This is causing the page to load twice for some strange reason. If I hard code a byte array without making this connection, the page loads once and all is well.

Here are the methods used to retrieve the external document. Maybe you can see something I can't.

public static byte[] GetDocument(string url)
        {
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            Stream stream = myHttpWebResponse.GetResponseStream();

            byte[] _Data = StreamToBytes(stream);

            return _Data;
        }

        private static byte[] StreamToBytes(System.IO.Stream theStream)
        {
            if (theStream == null)
                throw new ArgumentException("URL null.");

            int bytesRead = 0;
            byte[] buffer = new byte[8096];
            MemoryStream bufferStream = new MemoryStream();

            try
            {
                do
                {
                    bytesRead = theStream.Read(buffer, 0, 8096);
                    bufferStream.Write(buffer, 0, bytesRead);

                } while (bytesRead > 0);

            }
            finally
            {
                bufferStream.Flush();
                theStream.Close();
                theStream.Dispose();
            }

            return bufferStream.ToArray();
        }

The likely culprit is having the page directive of AutoEventWireup="true" in addition to OnInit() having this.Page_Load += Page_Load;

Auto Event Wireup does what it sounds like. If there is a method that follows the naming convention, the event is automatically wired up.

You also oftentimes see this on button handlers. The button handler will be set specifically, and the page will also create a button handler if the name follows the convention buttonname _OnClick(sender,args)

由于img标签的src为空,经常会发生此类问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM