Experience in asp.net website optimization and asp.net Optimization

Source: Internet
Author: User
Tags website performance

Experience in asp.net website optimization and asp.net Optimization

Recently, when a company launched a project, it often crashed, so it began to pay attention to performance issues. On the Internet, I saw many related articles and sorted them out. For reference.

 

When developing a website using ASP. NET, performance always needs to be considered and concerned. performance is not only the speed at which the program code is executed, but also related to all aspects.

Take ASP. for a request from the web browser to the server's ASP.. NET website sends the request until the end of the page is displayed in front of us. each step of the request has a different tuning method, and there are many calling methods, it is not just common: caching, multithreading, and Asynchronization.

The articles in this series decide to explain Optimization in two major aspects:

Front-end optimization: It mainly includes how to minimize http requests, from http requests to loading js and css, and how to compress transmitted data.

Background optimization: Analysis of ASP.. NET Request Processing Process, and the corresponding optimization method is provided in each step, and the optimization method is provided in the Code Organization, architecture and database operations.

I remember that when I was developing a website, when I mentioned how to improve performance, the easiest and fastest possible thing to think of was caching. In some Microsoft Best Practice documents, I also suggested: layer-by-layer caching (caching at the data storage layer, DAL, BLL, and UI ). Then, on the website, "the cache is blooming everywhere", and the final result is indeed unsatisfactory.

Another common optimization is for databases: for example, to minimize the number of subqueries, join is used; and indexes are created on fields that are frequently queried. Indeed, these are common and good rules.

Another experience is that, when optimizing performance, if you choose to optimize code and database, some database operations are often optimized to bring better results, but unfortunately: in projects (at least in some projects I have developed), databases are just a data storage device, and they have not played a powerful role in the database. Therefore, it is recommended that you be familiar with the internal query and storage mechanisms of databases. After all, many developers also work as DBAs (many companies do not have formal DBAs ).

In addition, when designing databases in projects, especially table fields, we need to consider some issues. Many people suggest that the length of table fields should not be too long. This is also a common recommendation, but why? In fact, we need to understand some internal storage mechanisms of the database: When the database (SQL SERVER) is saved, the data is in the smallest unit of "page, each page has a size of 8 K. If the data in your table exceeds 8 K, the data in this table will be saved on several pages. In this way, when querying the data, cross-page query is required. Cross-page query requires performance consumption. If data is on one page, the speed is certainly faster.

Therefore, to optimize the Website, you must know the performance consumption.

When optimizing a website, it is not blindly generalized. Generally, there are two situations:

1. The website already exists and runs. Now we need to optimize it.

2. A new website is being developed from scratch.

If this is the first case, we should first find out the bottleneck of the website performance, from the front-end request to the back-end request processing, until the presentation of the last page, all of which should be reviewed step by step.

In the second case, the situation may be slightly better, and the website is completely controlled by us now. Many optimization principles can be used in all development and design processes.

Optimization does not necessarily mean code rewriting or making major changes. A little bit of accumulation during optimization is like code refactoring, which is a cumulative effect. For example, whether to load the js script at the beginning of the page or load the js script at the end of the page, sometimes it is just a simple adjustment of the loaded file, or asynchronously load the script, or transmit the script through CDN, the performance is improved. The performance improvement is not zero, and some costs are very small. For example, if you just load the script to the end of the page, the big cost is, for example, buy some server devices, for example, Content Delivery Network (CDN) is used to transmit static files (js, css, and image) to the client. Therefore, optimization requires a balance strategy.

The following are some common problems and solutions for interaction between javascript and c #.

1. How to access the C # function in javaScript?

2. How to access the C # variable in Javascript?

3. How to access existing JavaScript variables in C?

4. How to access JavaScript Functions in C?

The answer to question 1 is as follows:

Execute functions in C # code in javaScript Functions:

Method 1: 1. Create a button to write the called or processed content to button_click in the background;

2. Write a js function at the front end. The content is

document.getElementById("btn1").click(); 

3. Call js functions on the frontend or backend to stimulate the click event, which is equivalent to accessing the c # function in the background;

Method 2: 1. function declaration is public

Background code (change public to PRotected)

1 public string ss() 2   { 3     return("a"); 4   }

2. can be called in html

Foreground script

1 <script language=javascript> 2 3   var a = "<%=ss()%>"; 4 5   alert(a); 6 7 </script>

Method 3: 1,

1 <script language = "javascript"> 2 3 <! -- 4 5 function _ doPostBack (eventTarget, eventArgument) 6 7 {8 9 var theForm = document. form1; // indicates runat = server's form 10 11 theForm. _ EVENTTARGET. value = eventTarget; 12 13 theFrom. _ EVENTARGUMENT. value = eventArgument; 14 15 theForm. submit (); 16 17} 18 19 --> 20 21 </script> 22 23 <input id = "Button1" type = "button" name = "Button1" value = "button" onclick =" javascript: __dopostback ('button1', '')">

Method 4:

1 <script language = "javascript"> 2 3 function SubmitKeyClick () 4 5 {6 7 if (event. keyCode = 13) 8 9 {10 11 event. cancelBubble = true; 12 13 event. returnValue = false; 14 15 document. all. funName. value = "Name of the function you want to call"; 16 17 document. form [0]. submit (); 18 19} 20 21} 22 23 </script> 24 25 <INPUT onkeypress = "SubmitKeyClick () "id =" aaa "type =" text "> 26 27 <input type =" hidden "name =" FunName "> <〈! -- Used to store the function you want to call --> --〉

In. CS, there are:

Public Page_OnLoad () {if (! Page. IsPost () {string strFunName = Request. Form ["FunName"]! = Null? Request. form ["FunName"]: ""; // you can call the switch (strFunName) {case "enter ()": enter () function based on the value returned (); // call this function break; case "other": // call other functions break; default: // call the default function http://www.cnblogs.com/sosoft/ break; }} public void enter () {//...... For example, calculating a value}

Question 2. How to access the C # variable in JavaScript?

The answer is as follows:

Method 1: 1. Hide domain access on the page

<input id="xx" type="hidden" runat="server"> 

Method 2: 1. If the backend defines public string n, the format of the variable referenced in front-end js is ''or" ++"

Method 3: 1. You can register a script on the page after assigning values to variables on the server.

"<Script language = 'javascript '> var temp =" + tmp + "</script>"

Tmp is the background variable, and then you can directly access temp in js to obtain the value.

Question 3. How to access existing JavaScript variables in C?

The answer is as follows:

Method 1: 1. the foreground uses static text controls to hide the field and write the js variable value into it. 2. The background uses request ["id"] to obtain the value;

Method 2: cookie or session can be used

Question 4. How to access JavaScript Functions in C?

The answer is as follows:

C # execute javaScript Functions in the Code:

Method 1: 1,

Page.RegisterStartupScript("ggg"," ");

Method 2: Use the Literal class and

1 private void Button2_Click(object sender, System.EventArgs e) 2   { 3     string str; 4     str=" "; 5     //Literal1.Visible=true; 6     Literal1.Text=str; 7   } 

 

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.