SlideShare a Scribd company logo
1 of 16
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

More Related Content

What's hot

ASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin LauASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin Lau
Spiffy
 
Ch 04 asp.net application
Ch 04 asp.net application Ch 04 asp.net application
Ch 04 asp.net application
Madhuri Kavade
 

What's hot (20)

Flux architecture
Flux architectureFlux architecture
Flux architecture
 
Comparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAsComparing Angular and React JS for SPAs
Comparing Angular and React JS for SPAs
 
ASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin LauASP.NET Overview - Alvin Lau
ASP.NET Overview - Alvin Lau
 
ASP.NET 4.0 Roadmap
ASP.NET 4.0 RoadmapASP.NET 4.0 Roadmap
ASP.NET 4.0 Roadmap
 
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web AppsSession 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
 
Ch 04 asp.net application
Ch 04 asp.net application Ch 04 asp.net application
Ch 04 asp.net application
 
ASP .NET MVC - best practices
ASP .NET MVC - best practicesASP .NET MVC - best practices
ASP .NET MVC - best practices
 
Sitecore MVC: What it is and why it's important
Sitecore MVC: What it is and why it's importantSitecore MVC: What it is and why it's important
Sitecore MVC: What it is and why it's important
 
Web api
Web apiWeb api
Web api
 
ASP.NET MVC 4 Request Pipeline Internals
ASP.NET MVC 4 Request Pipeline InternalsASP.NET MVC 4 Request Pipeline Internals
ASP.NET MVC 4 Request Pipeline Internals
 
Dot net interview questions and asnwers
Dot net interview questions and asnwersDot net interview questions and asnwers
Dot net interview questions and asnwers
 
ASP.NET Lecture 2
ASP.NET Lecture 2ASP.NET Lecture 2
ASP.NET Lecture 2
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
ASP.NET lecture 8
ASP.NET lecture 8ASP.NET lecture 8
ASP.NET lecture 8
 
Tutorial, Part 4: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 4: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...Tutorial, Part 4: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
Tutorial, Part 4: SharePoint 101: Jump-Starting the Developer by Rob Windsor ...
 
Esri Dev Summit 2009 Rest and Mvc Final
Esri Dev Summit 2009 Rest and Mvc FinalEsri Dev Summit 2009 Rest and Mvc Final
Esri Dev Summit 2009 Rest and Mvc Final
 
Simple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnanSimple mvc4 prepared by gigin krishnan
Simple mvc4 prepared by gigin krishnan
 
Advanced SharePoint Web Part Development
Advanced SharePoint Web Part DevelopmentAdvanced SharePoint Web Part Development
Advanced SharePoint Web Part Development
 
Web controls
Web controlsWeb controls
Web controls
 
Angular Data Binding
Angular Data BindingAngular Data Binding
Angular Data Binding
 

Similar to Introduction to ASP.Net MVC

Similar to Introduction to ASP.Net MVC (20)

Using MVC with Kentico 8
Using MVC with Kentico 8Using MVC with Kentico 8
Using MVC with Kentico 8
 
Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013Getting started with MVC 5 and Visual Studio 2013
Getting started with MVC 5 and Visual Studio 2013
 
ASP.NET MVC 2.0
ASP.NET MVC 2.0ASP.NET MVC 2.0
ASP.NET MVC 2.0
 
Asp 1-mvc introduction
Asp 1-mvc introductionAsp 1-mvc introduction
Asp 1-mvc introduction
 
ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!ASP.NET - Building Web Application..in the right way!
ASP.NET - Building Web Application..in the right way!
 
Aspnetmvc 1
Aspnetmvc 1Aspnetmvc 1
Aspnetmvc 1
 
Asp.net With mvc handson
Asp.net With mvc handsonAsp.net With mvc handson
Asp.net With mvc handson
 
MVC Framework
MVC FrameworkMVC Framework
MVC Framework
 
MVC 6 - the new unified Web programming model
MVC 6 - the new unified Web programming modelMVC 6 - the new unified Web programming model
MVC 6 - the new unified Web programming model
 
2011 NetUG HH: ASP.NET MVC & HTML 5
2011 NetUG HH: ASP.NET MVC & HTML 52011 NetUG HH: ASP.NET MVC & HTML 5
2011 NetUG HH: ASP.NET MVC & HTML 5
 
Mvc
MvcMvc
Mvc
 
Introduction to ASP.NET MVC
Introduction to ASP.NET MVC Introduction to ASP.NET MVC
Introduction to ASP.NET MVC
 
Asp.net mvc basic introduction
Asp.net mvc basic introductionAsp.net mvc basic introduction
Asp.net mvc basic introduction
 
Mvc fundamental
Mvc fundamentalMvc fundamental
Mvc fundamental
 
4. Introduction to ASP.NET MVC - Part I
4. Introduction to ASP.NET MVC - Part I4. Introduction to ASP.NET MVC - Part I
4. Introduction to ASP.NET MVC - Part I
 
Asp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin SawantAsp.net mvc presentation by Nitin Sawant
Asp.net mvc presentation by Nitin Sawant
 
Asp.net,mvc
Asp.net,mvcAsp.net,mvc
Asp.net,mvc
 
Aspnet mvc
Aspnet mvcAspnet mvc
Aspnet mvc
 
MVC architecture
MVC architectureMVC architecture
MVC architecture
 
Mvc Brief Overview
Mvc Brief OverviewMvc Brief Overview
Mvc Brief Overview
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 

Introduction to ASP.Net MVC

  • 2. 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
  • 3. 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.
  • 5. 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
  • 6. 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
  • 7. 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
  • 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. ASP.NET MVC Execution Life Cycle
  • 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 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
  • 14. 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(“...”)’
  • 15. 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
  • 16. 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