In this article we will explore some of the best practices in ASP.NET development.Best Practices - These are not rules, these are conventions which will be better if followed.Before starting I would like to request the reader to share here whatever ideas and best practices he/she uses.1. Choose pattern correctlyChoosing the correct pattern for your application development is a must.It's not always that 3-Tier, MVC, MVP or MVVM is best; if you have a fixed small size system which is not going to be extended in the future then 1-Tier/2-Tier will be good.I prefer at least 2-Tier.Always make a proper planning and understanding of the system before making any decisions.2. DataTierAlways follow some guidelines for SQL queries.
3. Application PoolCreate a new application pool for your site.4. App_Offline.htm fileMany people are unaware of this file. When you add a htm file name such as app_offline, every ASP.NET request will be redirected to this page.Use this page when you want to take your website offline temporarily, instead of writing your own logic for it.5. ValidationUse ASP.NET validation controls for validation because it supports both client side and server side validation.6. Page.IsValidAlways check for this property value in Control events like Button_click when validation is being done using Validation Controls because it's easy to bypass JavaScript code.7. ControlStateWhile developing user controls if maintaining state is a must then use control states instead of viewstate.8. Naming ConventionAlways follow a consistent Naming Guidelines and standards, which should include everything, from how to declare a variable to what the structure of a stored procedure will be.9. Foreach loopUsing a foreach loop instead of a "for" loop makes the code less cluttered.Use a "for" loop only when an index is required.10. Reference Type as Return TypesTry to understand the exact difference and usage of value and reference types.Take a look at the following examples:
Public int UpdateSalary(int Salary)
{
return Salary+1000;}.
.
Employee employee=//Get the Employee object
employee.Salary= UpdateSalary(employee.Salary);
Public Employee UpdateSalary(Employee e)
e.salary+=1000;
return e;}
Employee newEmployee=UpdateEmployee(oldEmployee);
Example 1 is perfectly fine but in example 2 oldEmployee and newEmployee refers to the same Employee. So it doesn't make any sense when a function returns a reference type which came to it as a parameter.11. NamespaceAlways do a clean coding.Include only necessary namespaces(In C# just right-click in Visual Studio, go to Organize Usings and click "Remove Unused Usings".)12. Abstraction and EncapsulationOOP concepts are the basis for every systematic development.
Maintain the standard of one function should contain one logic (purpose).13. CSS and JSNever use inline or internal CSS, go with an external.The same applies to JS.(When we include external css/js in the page it is cached and so wont download on every request. Two birds in a single shot Performance and Management.)14. Master PagesAlways use Master pages for consistent layout instead of frames and iframes.15. SessionAlways encapsulate session values as discussed in the following link:http://www.c-sharpcorner.com/UploadFile/SukeshMarla/Asp-Net-sessions-and-oops 16. ASP.NET Life cycleEvery ASP.NET developer must properly understand the various events exposed during the page life cycle. Each event has it s own significance and use; see:http://www.c-sharpcorner.com/UploadFile/SukeshMarla/Asp-Net-life-cycle/ Hope you enjoyed reading this article,I want you guys to share some more tips and thoughts related to same.If we share then only we can learn, I really appreciate to see some more tips here.Click here to read Best Practices for ASP.NET.