2. Webforms Pros
• Rich Controls (grids, etc…)
• RAD
• Event Driven (like Winforms)
• Stateful
• Less Control
• No built-in Separation of Concerns (SoC)
• Complicated Page life-cycle
• Not SEO friendly
URL
Big Viewstate at the beginning of the page
Webforms Cons
3. MVC Pattern
• What is MVC?
• Why MVC?
• Who uses MVC?
…“the concept of the design pattern in
software provides a key to helping developers
leverage the expertise of other skilled
architects.”
Grady Booch, 1995
4. MVC - Acronym for Model-View-Controller
MVC is a architectural pattern
Invented by Trygve Reenskaug in 1978
Originally used as an architectural pattern for building GUIs.
MVC Pattern
What is it?
5. Why MVC?
ASP.NET MVC Pros
More control over your html and JavaScript
Separation of Concerns (SoC)
SEO friendly
Test Driven Development (TDD)
Highly flexible & customizable
Not RAD
No rich controls
ASP.NET MVC Cons
6. MVC Pattern
Who uses it?
• .NET has Spring.NET and MonoRail
• Java has Swing, Struts, Grails and others
• Perl has Catalyst, Gantry, Jifty and others
• PHP has Zend, Zoop, Agavi and others
• Python has Django, Gluon, Pylon and others
• Ruby has Ruby on Rails
• Apple has cocoa and cocoa touch for objective-c [Mac, iPhone]
7. ASP.Net MVC Overview
• ASP.NET MVC?
• ASP.Net MVC Execution Process
• ASP.Net MVC 4 Project Structure
8. ASP.Net MVC Overview
ASP.Net MVC Architecture
M
Model
V
View
C
Controller
• Representation of domain data
• Business Logic
• Persistence mechanisms
• User Interface
• The representation of Model
• An intermediary between Model and View
• Application’s Brain (Handle user requests, bind
Data, return views)
11. ASP.NET MVC 4 Project
Structure
1. App_Data folder is the physical store for data.
2. Content folder keeps files such as scripts, CSS, images, etc
3. Controllers folder is the location for controllers.
4. Models stores classes that handle application domain logic.
5. Scripts folder is for script files. By default, this folder
contains AJAX script files and the JQuery library.
6. Views is the recommended location for views. View files
use .cshtml extension.
12. Routing R => the hidden characterMVC +R
1. Maps incoming URLs to the application and routes them to the right
Controller’s Action method to execute them
2. Happens in RegisterRoutes function in the global.asax file
16. Model
• It’s the core of your application
• A place for business logic, data access, Validation ... etc
• You can use your favorite data access technology with
ASP.NET MVC [LINQ to SQL, Nhibernate, Entity Framework
…etc]
18. Controller is a class containing a set of action methods.
Responsible for
o Handling user interaction
o Working with the models
o Selecting a view to render
o Sending model and information to a particular view
o Making decisions for security, UI, redirects ... etc.
The ASP.Net MVC requires the names of all controllers to end with the
suffix "Controller“ e.g. HomeController, LoginController & ProductController
Controller
19. Actions Rules:
1) Must be a public method
2) Cannot be a static method
3) Cannot be an extension method
4) Cannot be a constructor or property
5) Cannot have generic type.
6) Cannot contain ref or out parameters.
Controller
Controller Actions
An action is a method on a controller that gets called when you enter a
particular URL in your browser address bar.
20. Each action returns ActionResult which can be
• ViewResult => HTML and markup.
• EmptyResult => no result.
• RedirectResult => redirection to a new URL.
• JsonResult => json result that can be used in an AJAX application.
• JavaScriptResult => JavaScript script.
• ContentResult => text result [and wraps any non ActionResult return Type].
• FileContentResult => downloadable file [with the binary content].
• FilePathResult => downloadable file [with a path].
• FileStreamResult => a downloadable file [with a file stream].
A controller action always returns an ActionResult
Controller
Controller Actions
Note: all public methods of a controller class considered as action methods, if
you don’t want a public method to be an action, mark it with [NonAction()]
attribute
22. View
• Responsible for presentation, look & feel, formatting, sorting … etc.
• Interacts with model but doesn’t make any decisions
• No logic should go there
• No View State, No Server Controls
• HTML Helper functions
• Can be strongly typed
• Takes the view data from the controller
Tips:
• @ is equivalent to response.write()
• @{} is used to execute code
• Use @Html.Encode() method to prevent script injection
25. • Html.ActionLink()
• Html.BeginForm()
• Html.CheckBox()
• Html.DropDownList()
• Html.EndForm()
• Html.Hidden()
An HTML Helper is just a method that returns a string.
View
HTML Helpers
• Html.ListBox()
• Html.Password()
• Html.RadioButton()
• Html.TextArea()
• Html.TextBox()
e.g.
26. References
•Official website http://asp.net/mvc
•Source Code http://codeplex.com/aspnet
•Blogs
o Scott Guthrie http://weblogs.asp.net/scottgu
o Rob Conrey http://blog.wekeroad.com
o MVC Store Front http://blog.wekeroad.com/mvc-storefront
o Phil Haack http://haacked.com
o Scott Hanselman http://hanselman.com
o Stephen Walthler http://weblogs.asp.net/stephenwalther