ASP.NET Best Practices v2

1,122 views

Published on

ASP.NET Best Practices v2

Published in: Technology
  • Be the first to comment

  • Be the first to like this

ASP.NET Best Practices v2

  1. 1. ASP.NET Best PracticesV2For high Performance ApplicationsAlok Kumar Pandey
  2. 2. We discus about1. Practices for Application Development2. Practices for Production Applications
  3. 3. IntroductionASP.NET is very powerful technology, however it is important to understand how touse that power to build highly efficient, reliable and robust applications. In thisdiscussion we try to highlight the key tips that we can use to maximize theperformance of our ASP.NET pages. The above list can be much longer, we willdiscus the most important ones.
  4. 4. Plan and research before• Research and investigate how .NET can really benefit you.• .NET offers a variety of solutions on each level of application design anddevelopment.
  5. 5. String concatenation//using simple string concatenationstring strName = string.Empty;for (int i = 0; i < 10; i++){strName += "Name " + i;}//Using StringBuilderStringBuilder strBuilder = new StringBuilder();List<string> objNameList = new List<string>();objNameList = null;//Set The name listforeach (string strName in objNameList){strBuilder.Append(strName);}
  6. 6. Avoid round trips• Implement Ajax UI whenever possible• Use Client Side Scripts. Client site validation• Use Page.ISPostBack
  7. 7. Save viewstate only when neededViewState is used primarily by Server controls to retain state only on pages thatpost data back to themselves.Situation when we do not need ViewState• Our page does not post back.• We do not handle server control events.• We repopulate controls with every page refresh.Determine the size of your ViewState• By enabling tracing for the page, you can monitor the ViewState size for eachcontrol
  8. 8. Session variables- Avoid storing too much data in session variables- Make sure your session timeout is reasonable.To disable session state for a page,- Set the EnableSessionState attribute in the @ Page directive tofalse<%@ Page EnableSessionState="false" %>To disable session state for a specific application- Use Web.config file of the application.<sessionState mode=Off/>
  9. 9. Server.TransferIf authentication and authorization check is needed duringredirection• Use Response.RedirectTo transfer to pages in other applications• Must use Response.RedirectUse Server.Transfer to transfer control to pages in the sameapplication• Helpful to show the message like exception etc.
  10. 10. Server controls when appropriate and avoid creatingdeeply nested controls• Nothing comes for free• The HTTP protocol is statelessBetter to replace a server control to html control if1. You do not need to retain state across postbacks2. The data that appears in the control is static or control displays read-only data3. You do not need programmatic access to the control on the server-sideDeeply nested hierarchies create extra processing like• Repeater, DataList, and DataGrid
  11. 11. Choose the data viewing control appropriate foryour solution• Depend on how we choose to display data in a Web page• Always compare the pros and cons of controls before we use them in our Webpage
  12. 12. Optimize code and exception handling• To optimize expensive loops,• Use For instead of ForEach in performance-critical code paths.• Also do not rely on exceptions in code• Write code that avoids exceptions• Do not use exceptions to control logic• Check for null values, If it is possible for an object to be null
  13. 13. DataReader for fast and efficient data binding• Use a DataReader object if we not need to cache data• DataReader is the optimum choice for retrieving read-only data in a forward-only
  14. 14. Paging efficiently• Retrieves only the desired data from the database• Reduces back-end work on the database
  15. 15. Explicitly Dispose or Close all the resources• Cleaned up when an exception occurs, use a try/finally block• Open connection just before need• Close it as soon as done with it• Make sure to call the Dispose or the Close method of the object if provided
  16. 16. Disable tracing and debugging• Tracing and debugging are not recommended when our application is running inproduction.• We can disable tracing and debugging in the Machine.config and Web.configEg.<configuration><system.web><trace enabled="false" pageOutput="false" /><compilation debug="false" /></system.web></configuration>
  17. 17. Precompile pages and disable AutoEventWireup• Precompiled pages increases the performance,• Slight performance boost by leaving the event wiring to the page author insteadof performing it automatically.For example, we will need to override Page.OnLoad for the page load eventinstead of using a Page_Load method
  18. 18. Stored procedures and indexes• Use compiled stored procedures instead of RAW queries,• Proper indexes,• Reduce the chance of SQL Injection• Substitute the UNION operator by UNION ALL whenever possible.• Substitute Sub-queries by Joins• When in need of obtaining an amount of records form a table, avoid using theSelect Count(*) => table has a column called rows which stores the total amount ofrecords for each table of your database
  19. 19. Practices for Production Applications1. Generate new encryption keysReferencehttp://www.codeproject.com/Articles/16645/ASP-NET-machineKey-Generator2. Encrypt sensitive sections of your web.configThis includes both the connection string and machine key sections.Referencehttp://msdn.microsoft.com/en-us/library/68ze1hb2(VS.80).aspx3. Use trusted SQL connectionsReferencehttp://idunno.org/articles/276.aspx
  20. 20. Practices for Production Applications4. Set retail="true" in your machine.config<configuration><system.web><deployment retail="true"/></system.web></configuration>Referencehttp://msdn.microsoft.com/en-us/library/ms228298(VS.80).aspx5. Create a new application pool for your site6. Set the memory limit for your application pool7. Create and appropriately use an app_Offline.htm file
  21. 21. Practices for Production Applications8. Develop a repeatable deployment process and automate it8. configuration files that may be different between the development, staging, or productionenvironments.9. Build and reference release versions of all assemblies10. Load testthreading and memory issues
  22. 22. Thank you?

×