Click here to Skip to main content
Licence 
First Posted 18 May 2005
Views 32,793
Bookmarked 19 times

Best practices in ASP Dot NET

By | 18 May 2005 | Article
A quick reference on the "Best practices in ASP Dot NET"

Best practices in ASP.NET

                                                - by Raghunandan S

 

ASPX file

  • The labels / hard-coded values in the .aspx file should be fetched from the properties files / .xml files.
  • All the exceptions should be propagated to the end-user with a proper message.
  • Code should be modular and read-able.
  • Unwanted html tags should be removed from the .aspx files.
  • Unwanted breaks, blank spaces should be removed as they increase the data bandwidth.
  • 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>
  • Code should be indented with 2 spaces only (more spaces means more data bandwidth).
  • Use page-caching for frequently visited screens.
  • Use cascaded style sheets instead of having inline styles.
  • Avoid unwanted inclusions of .js files; include only the required .js files.
  • Avoid html frames as they are considered style of web designing. Instead of html frames, use include-files.
  • Avoid many hidden fields in the files, this adds on to the data bandwidth.
  • Always get the absolute path of the images or the include files.
  • Reduce the usage of Custom Tag (server controls) until and unless you are sure that the Custom Tag you are using are optimized.

 

 Code behind / class file

  • Module level variables: Module level variables (global to the class) should be avoided as they not thread-safe.
  • 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>
  • 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>
  • Disposing the Objects: Manually dispose the objects after use. Though .NET framework supports garbage collection, manual disposal increases the performance of the application.
  • 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.
  • Using Interfaces: Use Interface rather than implementation. For Eg: Use List instead of ArrayList, This will reduce the Typecasting of Objects.
  • View 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

RaghuOnDotNet

Team Leader
LnT Emsys
India India

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 Pinmemberaphazel0:36 16 Nov '09  
GeneralGood article Pinmemberrabashani23:53 27 Jan '06  
GeneralStringBuilder query PinmemberPhil Uribe12:43 19 May '05  
Questionwhere is the rule #1 ? Pinmemberdnh12:19 18 May '05  
GeneralMore explanation and justification needed PinmemberDavid Wulff6:26 18 May '05  
GeneralRe: More explanation and justification needed Pinmemberdnh11:47 18 May '05  
GeneralRe: More explanation and justification needed PinmemberDavid Wulff13:46 18 May '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 18 May 2005
Article Copyright 2005 by RaghuOnDotNet
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid