Download - ASP.Net MVC
Introduction To ASP.Net MVC New Framework in Old Style?
Ahmad ABU ARJAMCP, MCAD, MCSD, MCTS, MCPD Enterprise
Do you feel like this each time Microsoft announces a new technology?
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
• 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?
MVC PatternHow does it work?
Model
Controller
View
Flow Control
Domain Logic Data Persistence
Presentation[look and feel, formatting, sorting,
etc.]
Read only
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]
ASP.Net MVC Overview• ASP.Net MVC?• ASP.Net MVC Project Structure• ASP.Net MVC Execution Process
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
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.
ASP.Net MVC OverviewASP.Net MVC Execution Process
Request Perform routing
Create MVC request handler
Create controller
Execute controller Invoke action
Render View
DEMO
• Create New ASP.Net MVC Project• Investigate ASP.Net MVC Structure
RoutingR => the hidden character
MVC +R
• 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
RoutingTo customize routing rules
Model [M]
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]
Controller [C]Controlling the flow of application execution
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
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.
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
View [V]
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
• 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.
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
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