ASP.NET MVC: Views

Download Report

Transcript ASP.NET MVC: Views

Jess Chadwick
Lead Code Monkey
Infragistics Website Team
Todd Snyder
Development Team Lead
Infragistics Experience Design Group
ASP.NET MVC:
CONTROLLERS & VIEWS
Model – View - Controller
Model
Controller
View
Controller – The Traffic Cop
 Handles User Input
 Interacts with Model/Data
 Select View to Render
Controllers MVC - WebForm
ASP.NET Page Lifecycle
Request
Request Flow
HTTP
Routing
Http
Handler
Controller
Response
Route
Route
Handler
View
Engine
View
Controllers – Page Routing
Controllers – Actions
Controllers – Defining Actions
Controllers – Parameters
 RouteData: Contains Name/Value Pairs
 Form, Query String, and/or Cookie
 Missing Reference/Nullable Types set to null
 Nullable optional parameters
 Base class: Request and Response Methods
Controllers – Action Results
 ViewResult
 RedirectResult
 RedirectToRouteResult
 ContentResult
 JsonResult
 EmptyResult
Controllers – Attributes
 PrincipalPermission
 NonAction
 ActionFilter
Controllers – Action Filters
 Attach additional behaviors to actions
 Before & After Action Execution
 Before & After Result Execution
 Useful for logging, compression, etc.
Controllers – Unit Testing
Testing Controller Actions
 No requirement to test within ASP.NET
runtime!
 Use RhinoMocks, TypeMock, Moq, etc.
 Create Test versions of the parts of the runtime you want
to stub
[TestMethod]
public void ShowPostsDisplayPostView() {
TestPostRepository rep = new TestPostRepository();
BlogController controller = new BlogController(rep);
var result = controller.ShowPost(2);
Assert.AreEqual("showpost", result.ViewName);
Assert.IsTrue(repository.GetPostByIdWasCalled);
Assert.AreEqual(2, repository.LastRequestedPostId);
}
ASP.NET MVC: Views
Model – View - Controller
Model
View
Controller
Views – What are they?
 Render output
 Usually pretty “stupid”
Views - Working with Data
It’s all about the “ViewData”
Views - Strongly-Typed Data
 Inherits ViewPage<TModel>
 DEMO: Strongly-Typed View
Views - Weakly-Typed Data
 Inherits ViewPage
 DEMO: Weakly-Typed View
View Engines
“I can see!”
View Engines - Definition
 Figure out how to render the markup
 View Locator finds the template
 Pluggable
 Web Forms, Nvelocity, Brail, Nhaml, etc.
 Roll your own
 Implement IViewEngine
View Engines - WebFormViewEngine
 Default engine based on Web Forms markup
 Familiar Environment
 Layout: .master
 Content: .aspx
 User Controls: .ascx
View Engines - WebFormViewLocator
 Standard, default layout:
/Views
/{Controller Name}
/*.aspx
/*.ascx
/Shared
/*.master
/*.aspx
/*.ascx
UI Helpers
Think of them as your personal assistants.
Why do we need UI Helpers?
 More control = less done for you
 Avoid writing “boring” markup
 Encapsulate what varies
 Replace Web Forms server controls
Extension Methods Revisited
 New .NET 3.0 feature
 Static Methods posing as built-in behavior
 Make your life a whole lot easier
 Allow easy extensibility!
UI Helpers - Common/Useful Helpers
Form Helpers
Url/Link/Content Helpers
 Html.Hidden()
 Html.ActionLink<T>()
 Html.TextBox()
 Html.RouteLink()
 Html.TextArea()
 Html.Encode()
 Html.RadioButton()
 Html.AttributeEncode()
 Html.RadioButtonList()
 Url.Action()
 Html.DropDownList()
 Url.RouteUrl()
 Url.Encode(contentPath)
method(name, value, attributes)
 Url.Content(contentPath)
DEMO: Working with Forms
No, not <form runat=“server”>…
Back to basics!
Rendering Components
User Controls
 Extension Methods
 Html.RenderUserControl(virtualPath)
 Html.RenderUserControl(virtualPath, data)
 Server Controls
 Traditional Web Forms style!
 Third-Party Controls
 DEMO: Create and use a user control
Enriching the Client Experience
ASP.NET AJAX?
Nope!
Filling the Gap
 Roll your own
 Popular Frameworks
 jQuery
 prototype
 JavaScriptMVC
 Rico
 script.aculo.us
 ASP.NET AJAX (client library)
 DEMO: MVC & ASP.NET AJAX
Q&A
(thanks!)
Todd Snyder
Email: [email protected]
Blog: http://blogs.infragistics.com/blogs/tsnyder
Jess Chadwick
Email: [email protected]
Blog: http://blog.jesschadwick.com
Jess Chadwick
[email protected]
http://blog.jesschadwick.com
Resources
Todd Snyder
 The Bits
[email protected]
http://blogs.infragistics.com/blogs/tsnyder
ASP.NET MVC Preview 3: http://asp.net/MVC
ASP.NET MVC Source Code: http://www.codeplex.com/aspnet
 Quickstart

 http://quickstarts.asp.net/3-5-extensions/mvc/default.aspx
 Videos
 ASP.NET: http://www.asp.net/learn/3.5-extensions-videos/
 MIX: http://sessions.visitmix.com
 Community/Blogs
 ASP.NET Forums: http://forums.asp.net/1146.aspx
 Scott Guthrie (ScottGu): http://weblogs.asp.net/scottgu/
 Scott Hanselman: http://www.hanselman.com/blog/
 Phil Haack: http://haacked.com/
 Sample Apps
 MVC Samples: http://www.codeplex.com/mvcsamples
 CodeCampServer: http://codecampserver.org