Optimize. Net MVC to access pages slowly

Make a good ASP.NET MVC website, access speed is very slow, it takes several seconds to tens of seconds to display the page. Every dozens of minutes will occur such a situation. Below to share the optimization method.

 

The development environment is: VS2015 + IIS8+ SQL Server

Deployment environment: Windows 2008 R2+ IIS7+ SQL Server

 

Set IIS Recycler Pool Time Interval, or Set Recycling at a Specific Time

 

 

Set async as an asynchronous request if there is an ajax request on the page

In this way, if the interface responds slowly, even if it does not return to the page, it will be loaded first.

 

Setting Page Cache

     [OutputCache(Duration = 86400, Location = OutputCacheLocation.Any)]
        public ActionResult Index()
        {
            return View();
        }

  

Duration is to get or set the cache duration in seconds.

Location is to get or set a location.

 

Write programs to visit websites regularly

Because the caching time set above is 86400 seconds, the first visit of the website after 84600 seconds will still be slow, so we need to write a program to let him visit the website regularly, so as to avoid the slow first visit after the cache disappears.

 

Set the timer:

        Timer t = new Timer();
            t.Elapsed += new ElapsedEventHandler(TimedEvent);
            t.Interval = (1000 * 86400);
            t.Enabled = true;

  

Access method, this method may be slightly troublesome, because I see so directly copy. I don't write it myself.

 

   public static string PostAndRespStr(string url, int timeout = 5000, string postData = "", string contentType = "application/xml;", string encode = "UTF-8")
        {
            if (!string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(encode) && !string.IsNullOrEmpty(contentType) && postData != null)
            {
                HttpWebResponse webResponse = null;
                Stream responseStream = null;
                Stream requestStream = null;
                StreamReader streamReader = null;
                try
                {

                    return GetStreamReader(url, responseStream, requestStream, streamReader, webResponse, timeout, encode, postData, contentType);
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    if (responseStream != null) responseStream.Dispose();
                    if (webResponse != null) webResponse.Dispose();
                    if (requestStream != null) requestStream.Dispose();
                    if (streamReader != null) streamReader.Dispose();
                }
            }
            return null;
        }

  

        private static string GetStreamReader(string url, Stream responseStream, Stream requestStream, StreamReader streamReader, WebResponse webResponse, int timeout, string encode, string postData, string contentType)
        {
            byte[] data = Encoding.GetEncoding(encode).GetBytes(postData);
            HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

            webRequest.Method = "POST";
            webRequest.ContentType = contentType + ";" + encode;
            webRequest.ContentLength = data.Length;
            webRequest.Timeout = timeout;
            requestStream = webRequest.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            webResponse = (HttpWebResponse)webRequest.GetResponse();
            responseStream = webResponse.GetResponseStream();
            if (responseStream == null) { return ""; }
            streamReader = new StreamReader(responseStream, Encoding.GetEncoding(encode));
            return streamReader.ReadToEnd();
        }  

OK, first visit the server itself after publishing. Then try website speed.

Keywords: ASP.NET SQL encoding Windows IIS

Added by Yesideez on Thu, 13 Jun 2019 21:18:44 +0300