asp.net mvc

26
Introduction To ASP.Net MVC New Framework in Old Style? Ahmad ABU ARJA MCP, MCAD, MCSD, MCTS, MCPD Enterprise

Upload: ahmad-abu-arja

Post on 20-Nov-2014

6.900 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: ASP.Net MVC

Introduction To ASP.Net MVC New Framework in Old Style?

Ahmad ABU ARJAMCP, MCAD, MCSD, MCTS, MCPD Enterprise

Page 2: ASP.Net MVC

Do you feel like this each time Microsoft announces a new technology?

Page 3: ASP.Net MVC

MVC Pattern

• What is it?• How does it work?• Who uses it ?

…“the concept of the design pattern in software provides a key to helping developers leverage the expertise of other skilled architects.”Grady Booch, 1995

Page 4: ASP.Net MVC

• MVC - Acronym for Model/View/Controller• MVC is a architectural pattern • Invented by Trygve Reenskaug [1978]• Originally used as an architectural pattern for GUIs.• The first implementation of MVC was as part of the Smalltalk-80 class

library.

MVC PatternWhat is it?

Page 5: ASP.Net MVC

MVC PatternHow does it work?

Model

Controller

View

Flow Control

Domain Logic Data Persistence

Presentation[look and feel, formatting, sorting,

etc.]

Read only

Page 6: ASP.Net MVC

MVC Patternwho uses it?

• .NET has Spring.NET, 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]

Page 7: ASP.Net MVC

ASP.Net MVC Overview• ASP.Net MVC?• ASP.Net MVC Project Structure• ASP.Net MVC Execution Process

Page 8: ASP.Net MVC

ASP.Net MVC OverviewASP.Net MVC?

• A new web project type for ASP.NET.• It’s not a replacement for asp.net webforms, it’s a new option [flavor]

Advantages• Clean URLs and HTML• More control over your html and JavaScript• Friendly URLS• More easily Testable Framework.• Great integration within ASP.NET• All ASP.Net providers still work [Membership, Session, Caching, etc]

• Extensible and Pluggable [Support replacing any component]

• No more “postbacks” and “ViewStates”• Built on top of ASP.NET

Page 9: ASP.Net MVC

ASP.Net MVC OverviewASP.Net MVC Project Structure

App_Data folder is the physical store for data. Content folder keeps files such as scripts, CSS, images, etcControllers folder is the location for controllers.Models stores classes that handle application domain logic. Scripts folder is for script files. By default, this folder contains AJAX script files and the JQuery library.Views is the recommended location for views. Views use .aspx, .ascx, and .master files.

Page 10: ASP.Net MVC

ASP.Net MVC OverviewASP.Net MVC Execution Process

Request Perform routing

Create MVC request handler

Create controller

Execute controller Invoke action

Render View

Page 11: ASP.Net MVC

DEMO

• Create New ASP.Net MVC Project• Investigate ASP.Net MVC Structure

Page 12: ASP.Net MVC

RoutingR => the hidden character

MVC +R

Page 13: ASP.Net MVC

• Maps incoming URLs to the application and routes them to the right Controller’s Action method to execute them

Routing

Default Routing /Products /Products/Categories /Products/Detail/34

Page 14: ASP.Net MVC

RoutingTo customize routing rules

Page 15: ASP.Net MVC

Model [M]

Page 16: ASP.Net MVC

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]

Page 17: ASP.Net MVC

Controller [C]Controlling the flow of application execution

Page 18: ASP.Net MVC

Controller is a class consisting of a set of action methods.

Responsible for o Handling user interactiono Working with the modelso Selecting a view to rendero Sending model and information to a particular viewo 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, or ProductController

Controller

Page 19: ASP.Net MVC

Actions Rules:1) Must be a public method2) Cannot be a static method 3) Cannot be an extension method4) Cannot be a constructor or property5) Cannot have generic type.6) Cannot contain ref or out parameters.

ControllerController Actions

An action is a method on a controller that gets called when you enter a particular URL in your browser address bar.

Page 20: ASP.Net MVC

Action return 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

ControllerController 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

Page 21: ASP.Net MVC

View [V]

Page 22: ASP.Net MVC

View

•Responsible for presentation, look & feel, formatting, sorting … etc. •Interacts with model but shouldn’t make decisions •No logic should go there•Code Behind exists but not recommended•No View State, No Server Controls•HTML Helpers•Takes the view data from the controller•Can be strong typed •Can be extended by implementing View Engines

Tips:

•<%= %> is equivalent to response.write()•<%# %> is used to execute code •Take care of Html.Encode() method to prevent script injection

Page 23: ASP.Net MVC

• Html.ActionLink()• Html.BeginForm()• Html.CheckBox()• Html.DropDownList()• Html.EndForm()• Html.Hidden()

An HTML Helper is just a method that returns a string.

ViewHTML Helpers

• Html.ListBox()• Html.Password()• Html.RadioButton()• Html.TextArea()• Html.TextBox()

e.g.

Page 24: ASP.Net MVC
Page 25: ASP.Net MVC

References•Official website http://asp.net/mvc•Source Code http://codeplex.com/aspnet•Blogs

o Scott Guthrie http://weblogs.asp.net/scottguo Rob Conrey http://blog.wekeroad.como MVC Store Front http://blog.wekeroad.com/mvc-storefronto Phil Haack http://haacked.como Scott Hanselman http://hanselman.como Stephen Walthler http://weblogs.asp.net/stephenwalther

Page 26: ASP.Net MVC

Ahmad R. ABU ARJASoftware Designer email: [email protected]

T h a n k y o u

www.twitter.com/abuarja

www.linkedin.com/in/abuarja