10 issues to be aware of when developing applications using ASP. NET

Source: Internet
Author: User
Tags net command net command line

In this document, we will discuss how programmers use ASP. 10 tips that need to be paid attention to when developing applications. These skills involve changing the default control and form name to the use of stringbuilder class, which helps programmers adapt as soon as possible.. NET environment.
1. When using Visual Studio. NET, do not use the default name unless it is a direct or non-referenced object.

One of the benefits of. NET is that all source code and configuration files are plain text files and can be edited using any text editor such as NotePad or wordpad. If you do not want to use visual
Studio. NET is used as an integrated development environment. But with Visual Studio. NET, we can see the file in the Windows File Manager, or in the visual
Outside Studio. NET, you can browse the file content from the text editor.

Use Visual Studio
As an integrated development environment,. Net has many advantages. The most significant advantage is that it greatly improves production efficiency. Use Visual Studio.
Net, we can develop software faster at a low cost. As part of the integrated development environment, intelliisense provides Automatic Code Completion, dynamic help when entering methods or functions, real-time syntax error prompts, and other functions that can improve production efficiency.

Visual Studio
. Net can also make us feel frustrated. Sometimes, it is like a hard-to-understand black box, it will generate a large number of files and a lot of useless code.

Visual Studio
. NET provides a default name for a new object, whether it is an object in a class, control, or form. For example, if we create a new ASP. NET web
Application. The default name is webapplication1. In the "new project" dialog box, we can easily change the name of the application, but only the name of the application's namespace and its virtual directory are changed, the default names of source code files are still webform1.aspx and webform1.aspx. CS (C # project) or webform1.aspx. VB (VB.. Net project ).

We can change the file names used by aspx and code in the solution browser, but the webpage class name will still be webform1. If a button is generated on the web form, the default name is button1. In fact, the names of all controls are composed of the control type and number.

We can also change the names of all forms and controls in the application to meaningful names. For a small demo program, the default name is also competent, but if the application consists of multiple forms, each form contains many buttons and labels, form names such as frmstartup, frmdataentry, and frmreports are easier to understand and maintain than form1, form2, and form3.

If the form control needs to be referenced elsewhere in the code, it is more important to have a meaningful name. The names such as btnok, btncancel, and btnprint make it easier for code users to understand and maintain controls such as button1, button2, and button3.

A good way to modify a name in all files in a project is
In the. NET menu, choose Edit> discover and replace> replace.

When looking at the code we wrote two weeks ago, we often see the code for the first time, so it is necessary to make them have a name that helps us understand its meaning.

2. Even if you do not use Visual Studio. NET for programming, using code to support files can also improve application performance.

In all ASP. NET web projects such as Web applications, Web Services, and Web controls, Visual Studio
. Net all use code to support files. Code support files make the project better organized and coherent, and more suitable for development teams composed of multiple people. It also improves the performance.

The content of the Code support file is compiled into a class in the composite file, which is generally a DLL file and sometimes an EXE file. The file resides in the high-speed buffer of the application assembly, and can be obtained immediately when the application starts.

If the code is included in the <SCRIPT> mark or the aspx file code, it will still be compiled into a webpage class. In this case, every time the web page is loaded for the first time in the application conversation, it needs to be re-compiled, and the compiled class will reside in the memory. This file must be re-compiled whenever the computer is started, IIS is stopped, restarted, or the source code or configuration file is changed. Although not big, this causes considerable performance loss.

3. Minimize form delivery

Every time you click the button, linkbutton, or imagebutton control on a web page, the form is sent to the server. If the autopostback attribute of the control is set to true, if the status of the control such as checkbox and checkboxlist is changed, the form will be sent back to the server.

Each time a form is sent back to the server, it is reloaded, The page_load event is started, and all code in the page_load event processing program is executed. It is most appropriate to put the webpage initialization code here. We often want to execute some code each time a webpage is loaded, but we want to execute other code only when the webpage is loaded for the first time, I even want some code to be executed each time except for the first loading.

You can use the ispostback feature to complete this function. When a webpage is loaded for the first time, the value of this attribute is false. If the webpage is reloaded due to sending back, the value of the ispostback attribute is set to true. Through testing, you can execute the Specified Code at any time. The following is the relevant C # code:

Protected void page_load (Object sender, eventargs E)


// Operations performed each time a webpage is loaded

If (! Ispostback)


// The operation performed when the page is loaded for the first time




// The operation performed during the delivery.


// Operations performed when a webpage is loaded


We hope to avoid sending back as much as possible (the server is required to perform a series of operations each time), even if it causes sending back. We also hope to perform as few operations as possible. Large-scale and time-wasting operations (such as database searches) should be avoided in particular because they can prolong the application response time.

4. Use the stringbuilder class

The string is immutable In the. NET Framework, which means that the operators and methods for changing the string will return the modified copy of the string, which means there is still room for improvement in performance. When performing a large number of string operations, using the stringbuilder class is a good choice.

The following C # code tests the time required to generate a string from the 10000 substring in two ways. A simple String concatenation operation was used for the first time, and the stringbuilder class was used for the second time. To view the result string, remove the annotation symbol of the annotation line in the following code:

<% @ Page Language = "C #" %>

<SCRIPT runat = "server">

Void page_load (Object source, eventargs E)


Int int intlimit = 10000;

Datetime starttime;

Datetime endtime;

Timespan elapsedtime;

String strsub;

String strwhole = "";

// First perform the string connection operation

Starttime = datetime. now;

For (INT I = 0; I <intlimit; I ++)


Strsub = I. tostring ();

Strwhole = strwhole + "" + strsub;


Endtime = datetime. now;

Elapsedtime = endtime-starttime;

Lblconcat. Text = elapsedtime. tostring ();

// Lblconcatstring. Text = strwhole;

// Use the stringbuilder class for the same operation

Starttime = datetime. now;

Stringbuilder sb = new stringbuilder ();

For (INT I = 0; I <intlimit; I ++)


Strsub = I. tostring ();

SB. append ("");

SB. append (strsub );


Endtime = datetime. now;

Elapsedtime = endtime-starttime;

Lblbuild. Text = elapsedtime. tostring ();

// Lblbuildstring. Text = sb. tostring ();





<Form runat = "server">

<H1> String concatenation benchmark


<Asp: Label

Id = "lblconcat"

Runat = "server"/>


<Asp: Label

Id = "lblconcatstring"

Runat = "server"/>




<Asp: Label

Id = "lblbuild"

Runat = "server"/>


<Asp: Label

Id = "lblbuildstring"

Runat = "server"/>




The difference between the two methods is quite large: The append method using the stringbuilder class is nearly 200 times faster than the string connection.
5. Use server-side controls only when necessary

ASP. NET introduces a Web Server
Controls controls are often described in the code using the following syntax:

<Asp: textbox id = "txtlastname" size = "40" runat = "server"

They are also known as ASP controls. The server control is indicated by the runat attribute, and its value is always "server ".

By adding the runat attribute, the general HTML control can be easily converted to the server for running. The following is a simple example:

<Input type = "text" id = "txtlastname" size = "40"
Runat = "server"/>

You can use the name specified in the ID attribute to reference controls in the program. You can set attributes and obtain values programmatically. Therefore, server-side processing is flexible.

This flexibility has a certain price. Each Server Control consumes resources on the server. In addition, unless the view is explicitly disabled by the control, webpage, or application
State, the control state is included in the hidden field of view state, and will be passed in each delivery, which will cause serious performance degradation.

A good example in this regard is that for applications that control tables on webpages, if you do not need to reference the elements in the table in the code, you can use HTML tables that do not require server processing. We can still place server controls in HTML table cells and reference server controls in code. To reference any table element, such as a specified unit, the entire table must be a server control.

6. Differences between hyperlink controls and linkbutton controls

For Web visitors, the hyperlink and linkbutton controls are the same, but they still have major functional differences.

When a user clicks the control, the hyperlink control will immediately "navigate" the user to the target URL, and the table pieces will not be sent back to the server. The linkbutton control first sends the table to the server, and then navigating the user to the target URL. If server-side processing is required before the target URL is reached, use the linkbutton control. If server-side processing is not required, you can use the hyperlink control.

7. Comment on the code

This technique is not intended for ASP. NET, but it is a good programming habit.

Annotations should not only describe what operations the code performs, but also indicate the reason. For example, not just in comments, it indicates that the traversal array is to calculate a value based on an algorithm, unless the algorithm is quite simple, otherwise, the algorithm should be briefly described.

Different programming languages in the. Net project have different annotations. The following is a brief description:

HTML <! -- Comment -->

Javascript // Annotation

VBScript ''comment

VB. net'' Annotation

C # // Annotation

/* Multiline content



SQL -- Comment

There is no annotator In the start and end tags of the server control, but the server can ignore all attributes that it cannot recognize. Therefore, we can insert comments by using unspecified attributes. The following is an example:

<Asp: textbox

Id = "txtlastname"

Size = "40"

Comment = "this is my comment"

Runat = "server"/>

In Visual Studio
It is very easy to annotate the source code in. net. The lines to be annotated are displayed in High Brightness. Press Ctrl + K + C to add comments. To delete a comment, you only need to display the commented code with High Brightness and press Ctrl + K + U.

In the C # project, we can also use the // input XML comment section at the beginning of each line. In the annotations section, we can use the following XML tag to organize Annotations:

<Summary> </Summary>

<Remarks> </remarks>

<Param> </param>

<Returns> </returns>

<Newpara> </newpara>

In Visual Studio
To view the formatted reports of these XML annotations in. net, select the "Tools" menu item and select the "Create annotation web page" menu item.

8. Use the TRACE method and trace properties to record the execution of webpages in the page Directory.

An old technique of debugging a program is to insert an output statement into the key points of a program. Normally, the output information contains the values of important variables, relevant information can be output to the screen, log file, or database.

In ASP. NET, this debugging technology is easier to use by using the trace attribute in the page command. The page command is a line of code at the beginning of the aspx file, which provides the compiler instructions. The page command contains one or more attributes to provide the compiler with information such as the programming language used, the location of the Code support file, or the name of the class to be inherited.

One of the attributes in the page command is trace, whose values may be true or false. Below is a typical page command, where the trace attribute value is true:

<% @ Page Language = "C #" trace = "true" %>

If the trace attribute value is set to true, the web page generated by the aspx file is displayed. In addition to the webpage, a large amount of other information about the page is displayed. The information is displayed in the following table:

· The request details provide the session ID, request time, and request status code.

· Trace information
Contains a list of Trace logs and web page lifecycle steps in chronological order. You can also add custom information to it.

· The Control tree lists all controls on a webpage in a hierarchical manner, including the size of each control in bytes.

· The Cookies set lists all cookies created on this webpage.

· Header set HTTP headers and their values.

· Server variables are server environment variables related to the webpage.

Included in Trace
Tracking logs in the Information Section are the most useful. Here we can insert our own tracking commands. There are two methods in the trace class to insert the command in the trace log: trace. write and trace. warn, except trace. the warn command displays and traces in red. the write command is displayed in a black font, and they are the same. Below is a screen snapshot of the tracking log, which contains several trace. Warn commands.

The most convenient feature in tracking logs is that we can insert trace into the entire code during development and testing. write and trace. warn statements. When the application is finally delivered, you can change the trace attribute value in the page command to disable these commands from working, instead of deleting these output statements before deploying the application.

9. Use stored procedures

Microsoft SQL
Server and other modern relational databases use SQL commands to define and process queries. Submit an SQL statement or a series of SQL statements to SQL Server.
The server parses the command, creates a query plan, optimizes it, and executes the query plan. This takes a lot of time.

Stored procedures are a series of SQL commands pre-parsed and optimized by the query processor. These commands are stored and can be executed quickly. A stored procedure is also called sprocs. It can receive input parameters so that a single stored procedure can process a large range of specific queries.

Because sprocs is pre-parsed, it is more important for complex queries, and its query plan is pre-optimized. Therefore, calling the query process is much faster than executing SQL statements with the same function.

10. Use the. NET command line

The. NET command line tool runs in the Command Prompt window. To make the command executable, it must reside in the current directory of the command prompt, or by setting the PATH environment variable.

. Net
The SDK installs a menu item on the "Start" menu, which can open a command prompt window with the correct path environment variable. You can choose Start> program> Microsoft
Visual Studio. NET> Visual Studio. NET tools> Visual Studio
. Net command prompt to start the Command Prompt window.

By dragging this menu item from the menu to the desktop and pressing CTRL + C, you can copy the shortcut of this menu item to the desktop, which is very convenient to use.

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.