Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • ASP.NET MVC Do's and Dont's or Best Practices

    • 0
    • 4
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 1.05k
    Comment on it

    Hello Friends, MVC is now getting more popular day by day, So before dealing with MVC we need to understand some Do's and Dont's of it. This blog will help you to understand Do's and Dont's of ASP.NET MVC.

    ASP.NET MVC Do's and Dont's / Best Practices

    1. Old Version of JQuery and Third party references. While adding any third party tool like KendoUI, Angular js or even simple JQuery files sometime we wont add latest references for them which creates issues at later stage or somewhere having performance issues. Try using updated reference for these files.

    2. Bundling and Minification Bundling is a simple logical grouping of files that could be referenced by unique name and loaded with single http requestor, Basically it reduces the number of individual HTTP requests to server. Where as Minification is a process of removing unnecessary whitespace, line break,comments and other unnecessary characters including long variables name renaming to shorter like EmployeeFirstName to e, from code to reduce its size thereby improving load times.

    3. Dont use magic strings Dont use ViewData[key], but always create a ViewModel per each View, and use strongly-typed views like:- ViewPage< ViewModel>.

    4. User Base Controller Enforce your own Base Controllers (having security and other settings) to inherit from all controllers.

    5. Delete the sample code which is not needed Most of the application still have these code file which we never use and its a bad practice to keep demo code in your applications.

    6. Put all validation logic in the model. In MVC we preferred DataAnnotations for validation. These annotations are added as attributes to the properties of a model class. So you write the all input validation logic in model.

    7. Use HTMLHelper extension methods. The System.Web.Mvc.Html class contains useful HTML extension methods. These extension methods include helpers for:
      Form generation (BeginForm)
      Input field generation (checkbox, hidden, radio button, textbox)
      Link generation (ActionLink)
      XSS protection (Encode)

    8. Implement HandleUnknowAction and HandleError. The default response for an unknown action is 404 (Not Found) error. If you override the HandleUnknownAction class in a controller, you can implement a default view in this error. Additionally, put the HandleError attribute on an action and/or controller and you can provide a standard error view for when an uncaught exception is thrown.

    9. Generating code using Scaffolding There is a code generator for MVC application that can automatically generate code from the model. The advantage of using Scaffolding we can very easily generate crud ( create, read, update and delete) Operation code.

    10. Always validate anti-forgery token on every POST Always validate anti-forgery token on all your POST actions for better security. In ASP.Net MVC, you just add a [ValidateAntiForgeryToken] attribute.

    public ActionResult Information(string Information)
    // implementation logic 
    11.Use Areas in MVC In mvc Areas help you to group functionalities in to independent modules thus making your project more organized. It is much more helpful in big projects where we have large modules it helps us in separation of code.

    12. Use Extension Methods By using Extension Methods we can extend class method without using inheritance. This keyword is used as default parameter in Extension method. Static methods in static class are used to create Extension method.

    13. Cache Your Data Caching provides a mechanism of storing frequently accessed data and reusing that data. Practically, this is an effective way for improving web applications performance. When content is cached at the application, it can eliminate the database request and it also reduce the network traffic.
    public class HomeController : Controller
        public ActionResult Index()
                      return View();

 1 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: