Be in a file is medium,10 skill that the attention needs when we are using personnel of discussion process designing ASP.NET to develop applied program,These skill are involved from default accuse, the change of the name that express sheet arrives StringBuilder kind use,Conduce to process designing personnel can getting used to.NET environment as soon as possible.
1, when use Visual Studio.NET,Divide direct or outside be not quotative object,Do not use default name.
One of profit that.NET brings are all source code and configuration file it is simple text file,The text editor that can use the random such as Notepad or WordPad has an edition.If not be willing,We are not must use Visual Studio.NET to regard compositive development as the environment.But used Visual Studio.NET,We can manage in Windows file implement be pleasant to the eye to the file,Or in the content that the file browses from inside text editor besides Visual Studio.NET.
Use Visual Studio.NET has a lot of profit as compositive development environment,Among them the most distinct advantage is it improved manufacturing efficiency greatly.Use Visual Studio. NET,Software is developed quickly below the circumstance that we are paying lesser price.The IntelliSense that regards compositive development environment as one part offers voluntary code to finish, the real time that inputting a method or dynamic help, solecism provides when function hints,The function that can improve manufacturing efficiency with etc.
The tool with other and complex elephant in that way,The effect that how produces it adequately in the society and master its " characteristics " before,Visual Studio.NET also can make we produce a kind of setback to feel.Sometimes,Its as inscrutable as black box,Can generate many file and the code of a lot of no use.
A function of Visual Studio.NET is,No matter be kind, accuse or the object in expressing sheet,It can provide default name for new target.For example,If we founded a new ASP.NET Web Application,Its are default the name will be WebApplication1.We can be in " new project " the name that this application changes conveniently in the dialog box,But the name of the just should use a program name space that changes at the same time and the fictitious catalog that its are in,The default name of source code file remains WebForm1.aspx and WebForm1.aspx.cs (C# project) or WebForm1.aspx.vb (VB.NET project) .
The file name that we can change ASPX and code to use in plan browser,But Web page kind the name still will be WebForm1.If be on sheet of this Web watch,make a button,Its are default the name will be Button1.In fact,All accusing the name is by accuse of type and digital composition.
We can,Also should mix all watch sheet in using a program accuse the name revises significant name.To demonstrating sexual program lesserly character,Default the name still can be competent,But if use a program by many watches sheet,When there are a lot of pushbuttons and ticket on every watch sheet,As single as such watch of FrmStartup, FrmDataEntry and FrmReports name compares such name of Form1, Form2 and Form3 more understand easily and safeguard.
If accuse on watch sheet should cite in the other place of code,It is more important to make it has a significant name.The person that such name of BtnOK, BtnCancel and BtnPrint makes read code understands more easily,Consequently,Also be Button1, Button2, Button3 than the name such accusing safeguard more easily.
A of a name that appears in all files in revising a project good method is,Ordinal in menu of Visual Studio.NET choice %26quot; editor %26quot; - %26gt; %26quot; discover and replace %26quot; - %26gt; %26quot; replace %26quot; command.
When the code that before seeing two weeks, writes,We often see just like first time these code are same,Because this makes they have to conduce to us understanding its,the name of the meaning is very those who be necessary.
2, although do not use Visual Studio.NET to undertake process designing,Use code supports a file to also be helpful for improving the performance that uses a program
In Web application program, Web serves or Web accuses in waiting for all ASP.NET Web projects,Visual Studio.NET uses code to support a file.Code support file makes the project has better organization, modularity,The development group that suits much person composition more.Additional,It still meets those who bring function rise.
The content of code support file is compiled in assembling a document into kind,It is file of a DLL commonly,Also can be EXE file sometimes.This file is stationed in in staying in the assembly cache area that uses a program,When applied process is started,Can get it instantly.
If code is to include,be in %26lt;script%26gt; mark or in ASPX file code,It still can be compiled page of a Web kind.Below this kind of circumstance,Every time when this webpage is using to load of the first time in program dialog,Need to undertake compiling afresh,Be compiled kind can be stationed in in staying in memory.Every time the computer is started, IIS stops, be started afresh or when file of source code, configuration is changed,This file must be compiled afresh.Although not quite,But the performance loss that such causing also is quite considerable.
3, reduce watch sheet to answer as far as possible send
The Button that clicks Web webpage to go up every time, LinkButton or ImageButton accuse when,Watch sheet can be sent on the server.If accuse AutoPostBack attribute is installed to be True,If CheckBox, CheckBoxList accuse after condition is changed,Also can make the meeting that express sheet sends a server.
Be sent to answer a server when watch sheet every time,Meet by new to load,Incident of the Page_Load that start,All code in program of processing of executive Page_Load incident.Putting the initialization code of the webpage is the most appropriate here do not pass.We hope to be in via regular meeting a few code are implemented when to load webpage every time,And the hope implements another some of code when to load of webpage first time only,Hope a few code are in even except carry out outside to load when to load every time first.
Can use IsPostBack character to fulfil this one function.When to load of webpage first time,The value of this attribute is False.If the webpage sends because of answering and by new to load,The value of IsPostBack attribute can be installed to be True.Through the test,Designation code can be implemented in aleatoric moment.Relevant C# code is below:Protected Void Page_Load(Object Sender, eventArgs E)
{
/ / webpage every time when to load,Executive a few operationsIf (! IsPostBack)
{
/ / the operation that executes when to load of webpage first time
}Else
{
/ / wind the operation that when sending, executes
}
/ / the operation that the webpage executes when to load every time
}
We hope to be not caused as far as possible time send (answer every time send metropolis requirement server to undertake a series of operation) ,Although cause,answer after sending.Also hope to be able to be carried out operate less as far as possible.The operation of large-scale, wasteful time (example exactly the number is searched according to the library) ought to avoid especially,Because they can lengthen the sound that uses a program,answer time.
4, use StringBuilder kind
String is immutable in.NET frame,The operator that this means change string and the copy after the method can return the change of string,This means function to still have the space that increase.When undertaking many string is operated,Use StringBuilder kind compared good choice one kind namely.
The time that the C# code below checks to generate string place to need from inside 10000 stature string with two kinds of means.Used a simple string series connection to operate for the first time;Used StringBuilder the second times kind.Want to examine result string,Can the annotate symbol that the annotate in the code below take out goes:
%26lt;%@ Page Language=%26quot;C#%26quot; %%26gt;
%26lt;script Runat=%26quot;server %26quot;%26gt;Void Page_Load(Object Source, eventArgs E)
{Int IntLimit = 10000;
DateTime StartTime;
DateTime EndTime;
TimeSpan ElapsedTime;String StrSub;String StrWhole = "";
/ / carry out string join to operate above allStartTime = DateTime.Now;For (int I=0; I%26lt;IntLimit; I++ )
{StrSub = I.ToString();StrWhole = StrWhole + " " + StrSub;
}EndTime = DateTime.Now;ElapsedTime = EndTime - StartTime;LblConcat.Text = ElapsedTime.ToString();
/ / LblConcatString.Text = StrWhole;
/ / use StringBuilder kind have similar operationStartTime = DateTime.Now;
StringBuilder Sb = New StringBuilder();For (int I=0; I%26lt;IntLimit; I++ )
{StrSub = I.ToString();Sb.Append(" ");Sb.Append(strSub);
}EndTime = DateTime.Now;ElapsedTime = EndTime - StartTime;LblBuild.Text = ElapsedTime.ToString();
/ / LblBuildString.Text = Sb.ToString();
}
%26lt;/script%26gt;
%26lt;html%26gt;
%26lt;body%26gt;
%26lt;form Runat=%26quot;server %26quot;%26gt;
%26lt;h1%26gt;String Concatenation Benchmark%26lt;/h1%26gt;
Concatenation:
%26lt;asp:LAbelId="lblConcat "Runat="server"/%26gt;
%26lt;br/%26gt;
%26lt;asp:LAbelId="lblConcatString "Runat="server"/%26gt;
%26lt;br/%26gt;
%26lt;br/%26gt;
StringBuilder:
%26lt;asp:LAbelId="lblBuild "Runat="server"/%26gt;
%26lt;br/%26gt;
%26lt;asp:LAbelId="lblBuildString "Runat="server"/%26gt;
%26lt;/form%26gt;
%26lt;/body%26gt;
%26lt;/html%26gt;
The difference of two kinds of means is quite big:Use StringBuilder kind the rate of Append method is nearly 200 times rapidder than using string connective rate.Relative result is shown as follows:
(graph:Picture01)
5, using server end to accuse when necessary only
In ASP.NET new introduced a kind to be in what the server carries the Web Server Controls be callinged that move to accuse,In code,They often are shown through the grammar below:
%26lt;asp:TExtBox Id="txtLastName" Size="40" Runat="server" /%26gt;
They also are called ASP to accuse sometimes.Server end accuses be by what Runat attribute indicates,Its value always is " Server " .
Through adding Runat attribute,General HTML accuses can by very change server end to move conveniently,A simple case is below:
%26lt;input Type=%26quot;text%26quot; Id=%26quot;txtLastName%26quot; Size=%26quot;40%26quot; Runat=%26quot;server%26quot; /%26gt;
The name that can decide through Id attribute middle finger,We are adducible the program accuses mediumly,The means setting property that can pass process designing and obtain a value,Because of this,Server end handles means to have bigger flexibility.
This kind of flexibility is have fair price.The resource that every kinds of server end accuses a metropolis to use up a server to go up.Additional,Unless accuse, webpage or applied process prohibit explicitly View State,Accuse condition is to include in View State conceal inside the country,Answering the metropolis in sending to be delivered every time,This meeting causes serious function to drop.
A in this respect very good example is,The application that a form accuses on the webpage,If do not need the element in form cites in code,Use the HTML form that need not undertake the server carries processing.We are OK still the server is placed to accuse in HTML form unit,Cite in code the server accuses.If need to cite aleatoric form element,The unit that appoints for example,Whole form must be the server accuses.
6. HyperLink accuses, LinkButton accuses difference
To the person that Web is visited,HyperLink, LinkButton accuses it is same,But they still have bigger difference in functional respect.
Click when the user accuse when,HyperLink accuses a meeting to stand be about to user " navigation " to target URL,Watch won't answer send to the server.LinkButton accuses will express a hair to return a server above all,Go to user navigation next target URL.If be in " arrive " need undertakes the server carries processing before target URL,Use LinkButton to accuse;If need not undertake the server carries processing,Can use HyperLink to accuse.
7, annotate code
This one skill is not be aimed at ASP.NET,But it is a good process designing convention.
Annotate ought to explain what code can carry out to operate not just,Still ought to make clear a reason.For example,Explaining in annotate merely is in the array that all over all previous,Should showing enumerate group however is a basis some algorithm computation is worth,Unless algorithm is quite simple,Still ought to undertake brief specification to algorithm otherwise.
The different programming language in.NET project has the annotate sign that differs severally,A brief specification is below:
HTML %26lt; ! - - annotate- -%26gt;
Annotate of JavaScript //
Annotate of VBScript ' Annotate of VB.NET '
Annotate of C# //
/ content of * much travel
annotate
*/
SQL- - annotate
Accuse in the server begin and is annotate symbol in end mark,But server can oversight drops all attribute that it cannot identify,The attribute that accordingly we can not define through using will insert annotate.An example is below:
%26lt;asp:TExtBoxId="txtLastName "Size="40 "Comment=" the annotate that this is me "Runat="server" /%26gt;
Explanatory note has to source code in Visual Studio.NET very simple.The travel that tall brightness shows need explanatory note,Add explanatory note by Ctrl+K+C combination key next.Should delete annotate,Need tall brightness to show only by the code of annotate,Press Ctrl+K+U combines key.
In C# project,We still can pass the beginning in every to be in use / / / input XML annotate is nodular.In annotate bar,We can use the XML mark below to organize annotate:
%26lt;summary%26gt;%26lt;/summary%26gt;
%26lt;remarks%26gt;%26lt;/remarks%26gt;
%26lt;param%26gt;%26lt;/param%26gt;
%26lt;returns%26gt;%26lt;/returns%26gt;
%26lt;newpara%26gt;%26lt;/newpara%26gt;
The formatted report that should examine these XML annotate in Visual Studio.NET,We can choose above all " tool " dish is individual,Choose next " build annotate Web webpage " dish is individual.
8, the executive circumstance of the webpage in use Trace method and catalog of Page of Trace attribute record
A kind of when debug a program old technique is the crux in the program the dot inserts output statement,Normally the circumstance falls,The cost that important variable can include in exporting information,Pertinent information can output file of screen, log or database.
In ASP.NET,Through using Page to command medium Trace attribute,This kind of use that debugs a technology is simpler.Page command is a code that ASPX file begins to be in,It is offerred compile implement directive.One or more property is included in Page command,To compile implement the place that provides use programming language, code to support a file or should accede kind the information such as the name.
One of attribute in Page command are Trace,Its are worth a likelihood is True or False,The Page that a model is below commands,The value of among them Trace attribute is True:
%26lt;%@ Page Language=%26quot;c#%26quot; Trace=%26quot;true%26quot; %%26gt;
If the value of Trace attribute is installed for True,The Web page that makes by ASPX file can be shown come,Besides webpage itself,Also can show about a large number of other information of this page come.The form of face bar shows under these information in a piece of form:
The condition that · Request detail offers time of Session ID, request and request is piled up.
· Trace Information is included dog time is pressed in lifecycle of log, webpage early or late ordinal the list of each measure.Additional,OK also to add custom-built information among them.
The means that · charges a tree to divide administrative levels with a kind is listed on the webpage all accusing,Include every to accuse with byte calculative size.
All Cookie that · Cookies gather lists this webpage is founded.
Head of HTTP of · head gather and their value.
The Server environment related · Server variable and this webpage is variable.
Include dog mediumly in Trace Information bar the log is the most useful,Here we can insert ourselves dog command.Trace kind in have 2 methods can dogging the command is inserted in the log:Trace.Write and Trace.Warn,Besides Trace.Warn the command shows with gules font, Trace.Write command shows with black font outside,They are identical.It is to dog below the snapshot of a screen of the log,Include among them a few Trace.Warn command.
Dog the most convenient function in the log is us can be being developed and Trace.Write and Trace.Warn statement are inserted in whole code in test process,And when final consign uses a program,The cost that can pass property of the Trace in changing Page to command,Prohibit these commands are effective,And these need not be deleted to output a statement before deploy application software.
9, use memory process
The SQL Server of Microsoft and database of other and contemporary relation use SQL to the command is defined and handle inquiry.Statement of a SQL or a series of SQL statements refer SQL Server,SQL Server can undertake to the command analytic,Found to inquiry plans and undertake optimizing to it next,Carry out this inquiry to plan next,This needs many time.
Memory process is a series of by inquiry processor beforehand analytic with optimized SQL command,These commands can be risen by memory,Can get apace is carried out.Memory process also is called Sprocs,It can receive input parameter,Make a single memory course can be handled relatively the inquiry with specific large-scale.
Because Sprocs is beforehand by analytic,To complex inquiry more appear important,Its inquire what optimize beforehand to the plan is,Because this calls inquiry process to compare the SQL statement rate that executes same function,want to be gotten quickly much.
10, use.NET command goes
Tool of.NET command travel hints move in accord with window in the command.To make the command can be carried out,It must is stationed in in staying in the present list that commands clew accord with,Or through installing PATH environment variable.
.NET SDK is in " start " individual event of a dish is installed on menu,The window of command clew accord with that this dish individual event can open to install PATH environment variable correctly.We can be passed ordinal click %26quot; begin %26quot; - %26gt; %26quot; program %26quot; - %26gt; %26quot; Microsoft Visual Studio.NET %26quot; - %26gt; %26quot; tool of Visual Studio.NET %26quot; - %26gt; %26quot; accord with of clew of command of Visual Studio.NET %26quot; ,Window of accord with of clew of the command that start.
When pulling a desktop to go up through attending this dish individual event from menu,Bolt by Ctrl+C at the same time,OK get on the quick way copy of this dish individual event to the desktop,Use rise the meeting is very convenient.
...