How to Troubleshoot Apps for the Modern Connected Worker
ASP.NET MVC - Whats The Big Deal
1. Venketash (Pat) Ramadass
Systems Architect & Managing Director
emediaIT
pat.ramadass@emediait.com
http://patramadass.emediait.com
2. Separation of Concerns
MVC
ASP.NET MVC
Demonstrations of building simple ASP.NET
MVC functionality
Questions
3. Presentation logic, business logic and data
access logic are mixed together too often
Business logic code within the User Interface
Unmanagable code, goes against good OO
principles
Proper Separation of Concerns (SoC) solves
this by de-coupling the overall architecture
4. Stands for Model-View-Controller
It is a design pattern for software development
Designed to promote and enforce Separation
of Concerns
Model – Data Access and Business Logic
View – User Interface
Controller – Handles User Interaction and Model
Manipulation
5. User
User Interface Generates Events
View(s) Controller(s)
Provides Data Manipulates
Model
6. MVC Framework which Microsoft is adding to
ASP.NET
Alternative to ASP.NET, not a replacement
Can combine both in a single solution
Currently in public beta, but there are
production systems using it already
7. Clear separation of concerns (SoC)
Testability - support for TDD
Fine-grained control over HTML and
JavaScript
Intuitive URLs (Routing) – SEO Friendly
8. Functionality Webforms ASP.NET MVC
URLs Page names and URL Routing Programmatically -
Rewriting REST
Event Driven Yes No
Stateful Yes (Viewstate) No
Easily Unit Tested No Yes
Abstraction from HTML Yes No
Exact control over HTML No Yes
Supports ORM Yes Yes
Easy use of Third Party UI Yes Partial (No state/postback
controls controls only)
Works on Linux/Mono Yes (Partial) Not yet supported, but can be
made to work
The above is not intended to be a complete list, it simply highlights some key differences.
9. 6
View
Routing
Controller
1 2 5
Rules
3
1. HTTP Request
2. ASP.NET MVC Framework evaluates URL against registered
Routing Rules and forwards the request to a matching Controller
Model Data Store
4
3. Controller calls Model to build ViewData, if required
4. The Model returns the requested Data to the Controller
5. The Controller selects a View and passes the Model to the View
6. View renders and returns HTTP Response
10. Creating a new MVC Project
Folder Structure
Sample application included in ASP.NET MVC
Application Template
A URL does not equal a page
Understanding URL Routing
11. URL: /Product/Details/3
This URL is parsed into three parts like this:
▪ Controller = ProductController
▪ Action = Details
▪ Id = 3
The request is therefore routed to the Details() action
on the ProductController class, with a Id parameter of
3
Notice that the suffix Controller is tacked on to the
end of the Controller parameter.
12. Default Route
Default Controller: HomeController
Default Action: Index
Default Id: Empty String
URL: /Employee
This URL is parsed into three parts like this:
▪ Controller = EmployeeController
▪ Action = Index
▪ Id = “”
The request is therefore routed to the Index() action on
the EmployeeController class
13. Default Route
Default Controller: HomeController
Default Action: Index
Default Id: Empty String
URL: http://localhost, i.e. Without supplying a
further URL
14. In a blog application, you may want to handle
incoming requests that look like: /Archive/12-
25-2009. For this you would need a Custom
Route.
The order in which routes are added is
important
15.
16. A controller should only contain the bare minimum of logic
required to return the right view or redirect the user to another
action.
A controller is just a Class, inherits from
System.Web.Mvc.Controller
HomeController.cs has 2 methods called Index() and About(),
these are the actions it exposes.
The URL /Home/Index invokes the HomeController.Index()
method and the URL /Home/About invokes the
HomeController.About() method.
Any public method in a controller is exposed as a controller
action.
17. A method used as a controller action cannot be overloaded or static
A controller action returns action result.
Six standard types of action results:
ViewResult – Represents HTML and markup.
EmptyResult – Represents no result.
RedirectResult – Represents a redirection to a new URL.
RedirectToRouteResult – Represents a redirection to a new controller action.
JsonResult – Represents a JavaScript Object Notation result that can be used
in an AJAX application.
ContentResult – Represents a text result.
ContentResult is useful for returning plain text such as dates, integers etc
18. Instead of returning an action result directly, you
normally call one of the following methods on
the Controller base class:
View – Returns a ViewResult action result.
Redirect – Returns a RedirectResult action result.
RedirectToAction – Returns a RedirectToRouteResult
action result.
RedirectToRoute – Returns a RedirectToRouteResult
action result.
Json – Returns a JsonResult action result.
Content – Returns a ContentResult action result.
20. A view should contain only logic related to generating the user
interface.
A view contains the HTML markup and content that is sent to the
browser. A view is the equivalent of a page.
In general, to return a view for a controller action, you must create
a subfolder in the Views folder with the same name as your
controller. Within the subfolder, you must create an .aspx file with
the same name as the controller action.
The HomeController.Index() action returns a view located at the
following path: ViewsHomeIndex.aspx
The HomeController.About() action returns a view located at the
following path: ViewsHomeAbout.aspx
21. Adding a new View Page
Using HTML Helpers to Generate View
Content
Using View Data to Pass Data to a View
22. An MVC model contains all of your
application logic that is not contained in a
view or a controller. This should be all of
your business logic and data access logic
Aim for fat models and skinny controllers
23. Create a new Database
Create a new Model
Using LINQ in Controller Actions
Using the Repository Pattern
24. Why TDD?
Testing the View returned by a Controller
Testing the View Data returned by a
Controller
Testing the Action Result returned by a
Controller
25. ASP.NET 3.5 Installed
Visual Studio 2008
Microsoft ASP.NET MVC Installed
Download the current Beta
26. ASP.NET 3.5 Installed
Visual Studio 2008
Microsoft ASP.NET MVC Installed
Download the current Beta
27. More Involved Examples
Add/Edit/Delete and use of Server Controls
▪ Email me if you would like information on this
Custom HTML Helpers
You can create your own Helper Classes and Extension
Methods which return formatted HTML strings
Unity Dependency Injection / IoC Container
Framework for Dependency Injection and Controller
Factories
Stand alone version and bundled with Enterprise Library
4.0
28. Using Partials as Templates for formatting/rendering
For example a template for how a Movie is rendered
HTML.RenderPartial(string partialViewName, object
Model)
Action Filters
Attributes that can be applied to a Controller Action or
entire Controller
▪ Authorization
▪ Action Result
▪ Exception
▪ Custom
For example for Logging/Tracing
29. Upgrading existing code will take a long time, better
for Greenfields projects
For now, applications which require richer user
interfaces will require more work with MVC
Third party UI library vendors have started developing and
releasing updated controls
Some SEO issues, may require URL rewrite engine,
but can be done
Not officially supported on pre Windows Server
2003/IIS 6, but can work
30. If you are developing a new system it is worth
evaluating the pros and cons, it is not a silver
bullet however
ASP.NET MVC is much more in line with what is
happening elsewhere in the industry, for
example Ruby on Rails
We feel that the Pros outweigh the Cons in
general and are implementing ASP.NET MVC in
our new Web Applications wherever possible
31. http://www.asp.net/mvc/
Official Site
http://weblogs.asp.net/scottgu
Scott Guthrie
http://www.hanselman.com/
Scott Hanselman
http://quickstarts.asp.net/previews/mvc
Videos, Examples, Blogs
http://martinfowler.com/articles/injection.html
Repository/Dependency Injection Pattern
http://www.pnpguidance.net/Category/Unity.aspx
Unity
http://patramadass.emediait.com
This presentation, example code