Top Banner
ASP.NET MVC: CONTROLLERS & VIEWS Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group
37

Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Dec 25, 2015

Download

Documents

Tyrone James
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

ASP.NET MVC:

CONTROLLERS & VIEWS

Jess ChadwickLead Code MonkeyInfragistics Website Team

Todd SnyderDevelopment Team Lead

Infragistics Experience Design Group

Page 2: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Model – View - Controller

Model

Controlle

rView

Page 3: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Controller – The Traffic Cop

Handles User Input Interacts with Model/Data Select View to Render

Page 4: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Controllers MVC - WebForm

Page 5: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

ASP.NET Page Lifecycle

Page 6: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Request FlowRequest

Page 7: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Controllers – Page Routing

Page 8: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Controllers – Actions

Page 9: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Controllers – Defining Actions

Page 10: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

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

Page 11: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Controllers – Action Results

ViewResult

RedirectResult

RedirectToRouteResult

ContentResult

JsonResult

EmptyResult

Page 12: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Controllers – Attributes

PrincipalPermission

NonAction

ActionFilter

Page 13: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Controllers – Action Filters

Attach additional behaviors to actions Before & After Action Execution Before & After Result Execution

Useful for logging, compression, etc.

Page 14: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Controllers – Unit Testing

Page 15: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

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);}

Page 16: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

ASP.NET MVC: Views

Page 17: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Model – View - Controller

Model

ControllerView

Page 18: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Views – What are they?

Render output

Usually pretty “stupid”

Page 19: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

It’s all about the “ViewData”

Views - Working with Data

Page 20: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Views - Strongly-Typed Data

Inherits ViewPage<TModel>

DEMO: Strongly-Typed View

Page 21: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Views - Weakly-Typed Data

Inherits ViewPage

DEMO: Weakly-Typed View

Page 22: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

“I can see!”

View Engines

Page 23: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

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

Page 24: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

View Engines - WebFormViewEngine

Default engine based on Web Forms markup

Familiar Environment Layout: .master Content: .aspx User Controls: .ascx

Page 25: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

View Engines - WebFormViewLocator

Standard, default layout:/Views

/{Controller Name}/*.aspx/*.ascx

/Shared/*.master/*.aspx/*.ascx

Page 26: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Think of them as your personal assistants.

UI Helpers

Page 27: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Why do we need UI Helpers?

More control = less done for you Avoid writing “boring” markup Encapsulate what varies Replace Web Forms server controls

Page 28: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

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!

Page 29: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

UI Helpers - Common/Useful Helpers

Form Helpers Url/Link/Content Helpers

Html.Hidden() Html.TextBox() Html.TextArea() Html.RadioButton() Html.RadioButtonList() Html.DropDownList()

method(name, value, attributes)

Html.ActionLink<T>() Html.RouteLink() Html.Encode() Html.AttributeEncode() Url.Action() Url.RouteUrl() Url.Encode(contentPath) Url.Content(contentPath)

Page 30: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

No, not <form runat=“server”>…

Back to basics!

DEMO: Working with Forms

Page 31: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Rendering Components

Page 32: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

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

Page 33: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Enriching the Client Experience

Page 34: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

ASP.NET AJAX?

Nope!

Page 35: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

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

Page 36: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

Q & A(thanks!)

Jess ChadwickEmail: [email protected] Blog: http://blog.jesschadwick.com

Todd SnyderEmail: [email protected] Blog: http://blogs.infragistics.com/blogs/tsnyder

Page 37: Jess Chadwick Lead Code Monkey Infragistics Website Team Todd Snyder Development Team Lead Infragistics Experience Design Group.

The Bits ASP.NET MVC Preview 3: http://asp.net/MVCASP.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

Jess Chadwick

[email protected]

http://blog.jesschadwick.com

Todd Snyder

[email protected]

http://blogs.infragistics.com/blogs/tsnyder

Resources