Introducing ASP .NET MVC
Points for discussion
•Introduction to Asp.NET MVC
•Architecture of Asp.NET MVC
•Model
•View
•Controller
•Goals of ASP.NET MVC
•File Structure
•Page Life Cycle
•Global.asax
Introducing ASP .NET MVC
• ASP .NET Web Controls have many benefits
    • They allow web applications to be built rapidly
    • They provide a very rich user experience
    • They can easily be bound to data sources
• But there are many disadvantages
    • As functionality increases so does the number of post-
    backs
    • The amount of View State data can grow alarmingly
    • The underlying HTTP infrastructure is not hidden
• The most serious problems are architectural
    • ASP .NET actively discourages MVC based best practices.
Architecture of ASP .NET MVC
What is a Model?
•MVC model is basically a C# or VB.NET class
•A model is accessible by both controller and view
•A model can be used to pass data from Controller to
view
•A view can use model to display data in page
What is a View?
•View is an ASPX page without having a code behind
file
•All page specific HTML generation and formatting can
be done inside view
•One can use Inline code (server tags ) to develop
dynamic pages
•A request to view (ASPX page) can be made only from
a controller’s action method
What is a Controller?
•Controller is basically a C# or VB.NET class which
inherits System.Mvc.Controller
•Controller is a heart of the entire MVC architecture
•Inside Controller’s class action methods can be
implemented which are responsible for responding to
browser OR calling views.
•Controller can access and use Model class to pass data
to Views
•Controller uses ViewData to pass any data to View
Goals of ASP .NET MVC
•Enable proper unit and integration tests
   • Controllers can be tested without the browser
   • Data access layer can be ‘mocked out’
• Transparent   naming conventions
   • URL’s are simple and clean
•Take control over markup
•Build on top of ASP .NET infrastructure
   • But make the post-back style controls optional
MVC File Structure & File Naming Standards

•MVC uses a standard directory structure and file naming standards
which are a very important part of MVC application development.
•Inside the ROOT directory of the application, there must be 3
directories each for model, view and Controller.
•Apart from 3 directories, there must have a Global.asax file in root
folder, and a web.config like a traditional ASP.NET application.
ASP.NET MVC Execution Life Cycle
Page Life Cycle
• The ‘UrlRoutingModule’ receives the request
   •It creates and runs the correct ‘MvcHandler’
•The handler creates and runs a controller
   •Creation is via the current ‘ControllerFactory’
   •The entry point to the controller is ‘Execute’
        •A ‘ControllerContext’ is passed as a parameter
•The controller runs the appropriate method
   •This is found using standard reflection
   •A list of parameters can be passed in
•Control is passed to the current ‘ViewEngine’
   • Which (typically) builds and calls a server page
Global.asax
public class MvcApplication : System.Web.HttpApplication {
   public static void RegisterRoutes(RouteCollection routes) {
          routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
          routes.MapRoute(
              "Default", // Route name
              "{controller}/{action}/{id}", // URL with parameters
              new { controller = "Home", action = "Index", id = "" }
          );
   }

     protected void Application_Start() {
         RegisterRoutes(RouteTable.Routes);
    }
}
Passing Data Into The Server Page
•   The simplest mechanism for passing data to the server
    page is via the ‘ViewData’ object
    •This is a dictionary that holds arbitrary objects
    •The dictionary is populated in the controller and
    automatically passed to the server page by the
    ‘MvcHandler’
•Information can be retrieved via a key or via generics
    •E.g. ‘ViewData[“order”]’ or ViewData.Get<Order>()
•A more strongly typed solution is possible
    •An arbitrary object is passed back from the controller
    •The server page inherits from ‘ViewPage<T>’
        •Where ‘T’ is set to the type returned from the
        controller
Mixing MVC with Normal ASP .NET
•There is no problem combining MVC and Web Forms
    •As everything is based on the same infrastructure
• An ASP .NET Web Application can contain:
    •MVC based controllers, actions and views
    •ASP .NET pages containing HTML and Web Controls
    •Windows Communication Foundation based Web
    Services
• Some changes may be required to routing tables
    • MVC can be configured to ignore requests to existing
    files
    •But you may wish to add explicit exceptions to prevent
    unnecessary calls which access the file system
•You can do this via ‘routes.IgnoreRoute(“...”)’
Using AJAX in Web Applications
•MVC uses jQuery to implement AJAX
   •Using ‘Ajax.BeginForm()’ you can generate markup and
   scripts that support partial post-backs
        •It takes an ‘AjaxOptions’ parameter that holds the
        ids of elements to be updated and where updates
        should be placed
   •This can be detected via ‘Request.IsMvcAjaxRequest’
        •You then send changes via ‘return PartialView(...)’
•ASP .NET AJAX is not replaced
   •It remains a better option for:
        •Calling .NET based Web Services
        •Creating client-side only controls
   •Plus there is all the code in the control toolkit
Conclusion
• Uses MVC design pattern for separation of different
parts of the application
•More scalable
•More and easily extensible
•More testable for business logic.
•Supports EF like LINQ to SQL,ADO.NET EF Model,
Standard ADO.NET for readers and Datasets etc

Introduction to ASP.Net MVC

  • 1.
  • 2.
    Points for discussion •Introductionto Asp.NET MVC •Architecture of Asp.NET MVC •Model •View •Controller •Goals of ASP.NET MVC •File Structure •Page Life Cycle •Global.asax
  • 3.
    Introducing ASP .NETMVC • ASP .NET Web Controls have many benefits • They allow web applications to be built rapidly • They provide a very rich user experience • They can easily be bound to data sources • But there are many disadvantages • As functionality increases so does the number of post- backs • The amount of View State data can grow alarmingly • The underlying HTTP infrastructure is not hidden • The most serious problems are architectural • ASP .NET actively discourages MVC based best practices.
  • 4.
  • 5.
    What is aModel? •MVC model is basically a C# or VB.NET class •A model is accessible by both controller and view •A model can be used to pass data from Controller to view •A view can use model to display data in page
  • 6.
    What is aView? •View is an ASPX page without having a code behind file •All page specific HTML generation and formatting can be done inside view •One can use Inline code (server tags ) to develop dynamic pages •A request to view (ASPX page) can be made only from a controller’s action method
  • 7.
    What is aController? •Controller is basically a C# or VB.NET class which inherits System.Mvc.Controller •Controller is a heart of the entire MVC architecture •Inside Controller’s class action methods can be implemented which are responsible for responding to browser OR calling views. •Controller can access and use Model class to pass data to Views •Controller uses ViewData to pass any data to View
  • 8.
    Goals of ASP.NET MVC •Enable proper unit and integration tests • Controllers can be tested without the browser • Data access layer can be ‘mocked out’ • Transparent naming conventions • URL’s are simple and clean •Take control over markup •Build on top of ASP .NET infrastructure • But make the post-back style controls optional
  • 9.
    MVC File Structure& File Naming Standards •MVC uses a standard directory structure and file naming standards which are a very important part of MVC application development. •Inside the ROOT directory of the application, there must be 3 directories each for model, view and Controller. •Apart from 3 directories, there must have a Global.asax file in root folder, and a web.config like a traditional ASP.NET application.
  • 10.
  • 11.
    Page Life Cycle •The ‘UrlRoutingModule’ receives the request •It creates and runs the correct ‘MvcHandler’ •The handler creates and runs a controller •Creation is via the current ‘ControllerFactory’ •The entry point to the controller is ‘Execute’ •A ‘ControllerContext’ is passed as a parameter •The controller runs the appropriate method •This is found using standard reflection •A list of parameters can be passed in •Control is passed to the current ‘ViewEngine’ • Which (typically) builds and calls a server page
  • 12.
    Global.asax public class MvcApplication: System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = "" } ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } }
  • 13.
    Passing Data IntoThe Server Page • The simplest mechanism for passing data to the server page is via the ‘ViewData’ object •This is a dictionary that holds arbitrary objects •The dictionary is populated in the controller and automatically passed to the server page by the ‘MvcHandler’ •Information can be retrieved via a key or via generics •E.g. ‘ViewData[“order”]’ or ViewData.Get<Order>() •A more strongly typed solution is possible •An arbitrary object is passed back from the controller •The server page inherits from ‘ViewPage<T>’ •Where ‘T’ is set to the type returned from the controller
  • 14.
    Mixing MVC withNormal ASP .NET •There is no problem combining MVC and Web Forms •As everything is based on the same infrastructure • An ASP .NET Web Application can contain: •MVC based controllers, actions and views •ASP .NET pages containing HTML and Web Controls •Windows Communication Foundation based Web Services • Some changes may be required to routing tables • MVC can be configured to ignore requests to existing files •But you may wish to add explicit exceptions to prevent unnecessary calls which access the file system •You can do this via ‘routes.IgnoreRoute(“...”)’
  • 15.
    Using AJAX inWeb Applications •MVC uses jQuery to implement AJAX •Using ‘Ajax.BeginForm()’ you can generate markup and scripts that support partial post-backs •It takes an ‘AjaxOptions’ parameter that holds the ids of elements to be updated and where updates should be placed •This can be detected via ‘Request.IsMvcAjaxRequest’ •You then send changes via ‘return PartialView(...)’ •ASP .NET AJAX is not replaced •It remains a better option for: •Calling .NET based Web Services •Creating client-side only controls •Plus there is all the code in the control toolkit
  • 16.
    Conclusion • Uses MVCdesign pattern for separation of different parts of the application •More scalable •More and easily extensible •More testable for business logic. •Supports EF like LINQ to SQL,ADO.NET EF Model, Standard ADO.NET for readers and Datasets etc