Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Best practices Asp.net MVC

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 257
    Comment on it

    There are several points which we have to keep in mind while developing Asp.Net MVC application.

    Some of them are given below:

     

    1. Remove unnecessary references- When we create a project there are many references which are added automatically. We use only some of these references not all. So we have to remove unused references from our application. This will prevent the CLR to loading the unused references which means that we will reduce the startup time of our application.
    And after removing unused references we will reduce the risks of conflicts with namespaces.

     

    2. Set Initial Configuration-Some project required some initial configuration to run setup. So we have to set that initial configuration that is required to run setup. This code is usually write in Application_Start event handler.    

     

    3. Make all validation logic in the model- We have to put all validation logic at the model layer. This is the client side validation. We can use DataAnnotations to perform this validations. These annotations are added as attribute to property of model class.
    Here below is the example of this:

    public class Login
        {
            [Required(ErrorMessage = "UserName is required")]
            public string UserName { get; set; }

            [Required(ErrorMessage = "Password is required")]
            public string Password { get; set; }

        }

     

    4. Keep Controllers Thin- The purpose of controller is to handle user interaction.It read data from view and send data to the model.So there should be no business logic in controller.We should write only less number of lines that is necessary. That's we called it as thin controller.


    5. Never use “magic strings” - Never use ViewData["key"] in Views because these have hard coded keys and these never returns spelling mistake error at compile time. Always use strongly typed ViewModel.


    6. Use Strong-Typed ViewModels where applicable- Wherever possible  we have to use strong typed ViewModels. It makes handling your data easier, provide compile time checking and provides intellisense  for developing HTML view.


    7. Use Data Annotations for Validation- Using data annotations we can define rules that we have to apply to our Model properties by adding Data Annotations to Models classes. We can enable validations by adding different attributes. for eg: Required, Range etc.


    8. Use Request Validation-Request validation is a technique that work on HTTP request and check whether it contain any malicious content or not. Malicious content is either any HTML markup or any javascript code. Request validation prevents us from form submitting data that contain potentially malicious content.


    9. Add Html.Encode for all user input data- HTML is encoded by using this method .This method is used to protect against Cross site scripting attacks (XSS).


    10. Use the Html.AntiForgeryToken- Use Html.AntiForgeryToken class to protect against the cross site request forgery (XSRF). On action of post request add ValidateAntiForgeryToken attribute.


    11. use parametrized SQL queries- Parametrized SQL queries is used to protect from the SQL injections attacks.


    12. Use partials-Partials is like UserControls in asp.net web forms. We can use partials to update only some part of page without reloading the complete page.


    13. Make routes as simple as possible- MVC Routing is very important to move from one URL to another through controller.We should make the routes as simple as possible. Remove extra routes as many as possible. Therefore design your routes carefully.


    14. If there is an if statement in views then use HtmlHelper- There should be no business logic in UI representation. If there is an "if" statement then use HtmlHelper to hide conditional statements.


    15. Use filters for adding behaviours- We can add filters by using attributes for action result and action methods. These filters attributes extend filtering capabilities. There are different types of filetrs in MVC.
    a. Authorization filters- This filter make security decision.
    b. Action filters- This filter is used to run additional processing.
    c. Result filters- This filter run after the result to perform additional processing of the result.
    d. Exception filters- This filter is used for logging and display error message.

     

    16. Use HTMLHelper extension methods- When we create HTML page, it is very helpful to use HtmlHelper class. This class support of rendering of HTML controls in a view.Also, it will work on all view engine that means it is portable between applications.


    17. Use interfaces for data access- We have to use interfaces to expose methods for data access. Using interfaces we can make the application loosely coupled so that it will to do changes.


    18. Bundling and Minification- These are the techniques that improves request load time because it reduce the number to request to the server and reduce the size of requested assets.
    Bundle is a group of files that loaded in single HTTP request.
    Minification is a technique that remove unnecessary characters from javascript and css files to reduce the size of file that improves load time of web page.


    19. Cache Data- We can use cache to improve the performance and responsiveness of our application. Caching is a technique in which we store data in memory and use this data for doing other functionality of the application.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
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: