Best practices in ASP.NET<o:p>
- by Raghunandan S<o:p>
<o:p>
ASPX file<o:p>
- The labels / hard-coded values in the .aspx file should be fetched from the properties files / .xml files. <o:p>
- All the exceptions should be propagated to the end-user with a proper message. <o:p>
- Code should be modular and read-able. <o:p>
- Unwanted html tags should be removed from the .aspx files. <o:p>
- Unwanted breaks, blank spaces should be removed as they increase the data bandwidth. <o:p>
- While using commenting style for JavaScript functions use server side commenting instead of the usual HTML comments, This reduces the data bandwidth
Server side comments : <code><% %></code>
Client side comments : <code> //<code> or <code><!-- --></code> <o:p>
- Code should be indented with 2 spaces only (more spaces means more data bandwidth). <o:p>
- Use page-caching for frequently visited screens. <o:p>
- Use cascaded style sheets instead of having inline styles. <o:p>
- Avoid unwanted inclusions of .js files; include only the required .js files. <o:p>
- Avoid html frames as they are considered style of web designing. Instead of html frames, use include-files. <o:p>
- Avoid many hidden fields in the files, this adds on to the data bandwidth. <o:p>
- Always get the absolute path of the images or the include files. <o:p>
- Reduce the usage of Custom Tag (server controls) until and unless you are sure that the Custom Tag you are using are optimized.<o:p>
<o:p>
Code behind / class file<o:p>
- Module level variables: Module level variables (global to the class) should be avoided as they not thread-safe. <o:p>
- String concatenation: Avoid String concatenation, use StringBuilder instead. Always initialize the StringBuilder with an approximate value. This will give a better performance since the memory extension calls will be reduced.
<code>stringBuilder = New StringBuilder (500)</code> <o:p>
- Variable initialization: Do not initialize any variable unless you reach a condition where the object is necessary.
<code>
String strName = Nothing
if(intId == 1)
{
strName = "Raghu"
}
</code><o:p>
- Disposing the Objects: Manually dispose the objects after use. Though .NET framework supports garbage collection, manual disposal increases the performance of the application. <o:p>
- Hard-coded constants: Do not hard-code any string constant value or numeric constant value. Assign the value to a constant (give a meaningful name which represents the constant value) and use. <o:p>
- Using Interfaces: Use Interface rather than implementation. For Eg: Use List instead of ArrayList, This will reduce the Typecasting of Objects. <o:p>
- <st1:place><st1:placename>View <st1:placetype>State: There are lots of drawbacks to the use of view state, however. First of all, it increases the data bandwidth of the page both when served and when requested. There is also an additional overhead incurred when serializing or de-serializing view state data that is posted back to the server. Lastly, view state increases the memory allocations on the server.
Several server controls, the most well known of which is the DataGrid, tend to make excessive use of view state, even in cases where it is not needed. The default behavior of the ViewState property is enabled, but if you don't need it, you can turn it off at the control or page level. Within a control, you simply set the EnableViewState property to false, or you can set it globally within the page using this setting:
<code><%@ Page EnableViewState="false" %></code>
If you are not doing postbacks in a page or are always regenerating the controls on a page on each request, you should disable view state at the page level.<o:p>
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.