Date post: | 06-May-2015 |
Category: |
Documents |
Upload: | barry-gervin |
View: | 8,416 times |
Download: | 1 times |
Introduction to ASP.NET MVCBarry Gervin, [email protected]@bgervinObjectSharp.com/blogs/Barry
Objectives
• Why do we need something else?– What’s wrong with Web Forms?
• “How To” build ASP.NET MVC– How does this compare? Help?
• What should I beware of?
Agenda
• WebForms• MVC• ASP.NET MVC– Tenets
• WebForms Vs. MVC• Walkthrough Real Application• Testing MVC• Extensibility
ASP.NET WEB FORMS
ASP.NET Then…
CachingCaching ModulesModules
HandlersHandlersIntrinsicsIntrinsics
PagesPages ControlsControls
GlobalizationGlobalization
ProfileProfile
Master PagesMaster Pages
MembershipMembershipRolesRoles
Etc.Etc.
ASP.NET
One web applicationframework to rule them all…
WebFormsASPX Page
User/Custom/ServerControls
Master Page
User/Custom/ServerControls
Model
QUICK DEMOPages, Events and Postbacks
Page Controller
ASPX Page
ASP.NET
Response
Request
Master Page
Control
Control
No real role responsibility…
UIPresentation LogicBusiness LogicData Access
Who does what?How and when?
Control
ControlPage
Control
Control
Control
Control
Control abstractions can be negative…
It isn't easy enough to test…It isn't easy enough to test…
Logic UI
WebForms are great, but options are
good…
ASP.NET MVC
ASP.NET Now…
ASP.NETDynamic Data
ASP.NETWebForms
ASP.NETMVC
Presentation
RuntimeASP.NET
Core
So how does ASP.NET MVC
differ?
MVC = Model-View-Controller
ControllerController(Input)(Input)
ModelModel(Logic)(Logic)
ViewView(Presentation)(Presentation)
Separation of concerns!
ASP.NET MVC
Model
ASPX Page
User/Custom/ServerControls
Master Page
User/Custom/ServerControls
Controller
How does MVC look?
Request
View
Controller
Response
ControllerHandles input(HTTP requests)
ViewVisually representsthe model
Framework Goals
• Alternative
• Frictionless Testability
• Tight control over <markup>
• Leverage the benefits of ASP.NET
• Routable Urls to Controllers to Views
• Conventions and guidance
• Modular and Extensible
Project Structure
Routes
Views/Master
Views/Master
Models
Controllers
ASP.NET MVC DEMORoutes, Controllers & Views
ASP.NET MVC doesn’t have…
• Postbacks• View state• Control state• Server-side form• Page/Control lifecycle
ASP.NET MVC still has…• Web designer• Master pages• User controls• Membership/Roles/Profile• Globalization• Caching• HTTP intrinsics:– HttpContext– HttpRequest– HttpResponse– Etc.
Routing
mysite.com/object/action/parameter
Clean URLs
Don’t settle for…
/Products.aspx?CategoryID=123
When you can easily have…
/Product/Puppies
Or whatever else makes sense…
protected void Application_Start(object sender, EventArgs e) {
RouteTable.Routes.Add("SearchRoute", new Route("search/{searchterm}", new WebFormRouteHandler("~/search.aspx")));
RouteTable.Routes.Add("UserRoute", new Route("users/{username}", new WebFormRouteHandler("~/users.aspx")));
} .... protected void Page_Load(object sender, EventArgs e) {
string searchterm= Page.RouteData.Values["searchterm"] as string; Response.Write(searchterm); }... <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="<%$RouteUrl:SearchTerm=Bob%>">Link to search</asp:HyperLink>
Routing
Controller Conventions• Controller– Must…
• Be suffixed with “Controller”• Implement IController (or inherit from Controller)
• Action– Must…
• Be Public• Return ActionResult or void
– Can’t…• Be generic• Have a NonActionAttribute• Have out/ref parameters
REAL WORLD WALKTHROUGHSayITFirst.ca
Extensibility Pipeline
ControllerBuilder
ControllerFactory
Controller
ViewEngine
ViewLocator
View
ControllerActionInvoker
ActionResult*
ActionFilters
* ViewResult
Action Results
• ActionResult– ContentResult– EmptyResult– JsonResult– RedirectResult– RedirectToRouteResult– ViewResult
ControllerController
ActionInvoker
ActionResult
Action Filters• IActionFilter– Controller– ActionFilterAttribute
1. OnActionExecuting2. [Action Method Executed]3. OnActionExecuted4. OnResultExecuting5. [Action Result Executed]6. OnResultExecuted
Controller Factory
• IControllerFactory– DefaultControllerFactory
ControllerBuilder
ControllerFactory
Controller
View Engine
• IViewEngine– WebFormViewEngine
• IViewLocator– ViewLocator
• WebFormViewLocator
ViewEngine
ViewLocator
ViewActionResult*
ASP.NET MVC 2
• A good number of new features including:– Client-side validation based on the model’s
validation metadata– Areas to better organize an application– Model validation providers to hook in alternative
validation logic when model binding– Metadata providers to allow for alternative
sources of metadata for model objects
Microsoft.com/web
Web Platform Installer
NerdDinner (nerddinner.com)
Professional ASP.NET MVC 1.0,
a.k.a. The “Gang of Foreheads” Book
ASP.NET MVC in Action
@ManningBooks
Pro ASP.NET MVC Framework
(Highest rating on Amazon.com)
The Official ASP.NET MVC SiteASP.NET/MVC
You’ve Been Haacked (Phil Haack) Haacked.com
Scott Hanselman’s BlogHanselman.com
ObjectSharp.com
ResourcesMicrosoft Starting Points•http://microsoft.com/web•http://asp.net/mvc•http://codeplex.com/MVCContrib•http://aspnet.codeplex.com
Community Starting Points•http://channel9.msdn.com/tags/asp.net+mvc•http://haacked.com/•http://stackoverflow.com
Summary
• Web Forms vs. MVC• 101 How-To• Real World ASP.NET MVC• Testability