Sunday, 4 September 2011

Notes from Pro ASP.Net MVC 2

  • ASP.NET MVC—generated pages don't contain any ViewState data, so they can be hundreds of kilobytes smaller than typical pages from ASP.NET Web Forms
  • ASP.NET MVC applications work well with UI automation testing tools. You can write scripts that simulate user interactions without having to guess what HTML element structures, CSS classes, or IDs the framework will generate or when it will change them.
  • The Ruby community has created and popularized a new generation of BDD-oriented UI automation technologies (e.g., Cucumber and WebRat - this guy has a good example of working with both); these ideas are now slowly leaking into the .NET world.
  • Controllers
    In model-view-controller (MVC) architecture, incoming requests are handled by controllers. In ASP.NET MVC, controllers are just simple C# classes (usually inheriting from System.Web.Mvc.Controller, the framework's built-in controller base class). [2] Each public method on a controller is known as an action method, which means you can invoke it from the Web via some URL.
  • Model Binding
    An extremely useful feature of ASP.NET MVC whereby incoming data is automatically parsed and used to populate action method parameters by matching incoming key/value pairs with the names of properties on the desired .NET type
    • Example:
      In your controller you have an Action Method "MyForm" which takes an instance of a bespoke class "MySpecialObj".
      What is special here is that "MyForm" responds directly to HttpPost requests...
    • [HttpPost]
      public ViewResult MyForm(MySpecialObj mySpecialObj)
            // This line tells ASP.NET MVC to find and render a view called       
            // DifferentView and to supply the mySpecialObj object to that view. 
            // Since this all happens in a controller called HomeController, 
            // ASP.NET MVC will expect to find the DifferentView view at 
            // ~/Views/Home/DifferentView.aspx
            return View("DifferentView", mySpecialObj);
    • Because the input controls defined in MyForm.aspx render with names corresponding to the names of properties on "MySpecialObj", the framework will supply to your action method a "MySpecialObj" instance already fully populated with whatever data the user entered into the form. 

No comments:

Post a Comment