Optimized. Net MVC slow page access and optimized. netmvc page

Source: Internet
Author: User

Optimized. Net MVC slow page access and optimized. netmvc page

Make an ASP. net mvc website, the access speed is very slow, it takes several seconds to dozens of seconds to display the page. this happens every few minutes. the following describes the optimization methods.

 

The development environment is VS2015 + IIS8 + SQL Server

The deployment environment is Windows 2008 R2 + IIS7 + SQL Server

 

Set the IIS recycle pool time interval, or set to recycle at a specific time

 

 

If there is an ajax request on the page, set async to an asynchronous request.

In this case, if the interface response is slow, even if no page is returned, it will be loaded first.

 

Set 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 the Location.

 

Write programs to regularly access the website

Because the cache time set above is 86400 seconds, if the website is accessed for the first time after 84600 seconds, it will still be slow, so we need to write a program for him to access the website regularly, avoid the first slow access 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 a little troublesome. I copied it directly because I saw it. You cannot write it yourself.

 

   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. After the release, access the server first. Then try the website speed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.