Published on

Published in: Technology
1 Comment
No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Introduction To ASP.Net MVC New Framework in Old Style Ahmad ABU ARJA MCP, MCAD, MCSD, MCTS, MCPD Enterprise
  2. 2. Do you feel like this each time Microsoft announces a new technology?
  3. 3. 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
  4. 4. MVC Pattern What is it? • 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.
  5. 5. MVC Pattern How does it work? Domain Logic Presentation Data Persistence [look and feel, formatting, sorting, etc.] Model Read only View Controller Flow Control
  6. 6. MVC Pattern who 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]
  7. 7. ASP.Net MVC Overview • ASP.Net MVC? • ASP.Net MVC Project Structure • ASP.Net MVC Execution Process
  8. 8. ASP.Net MVC Overview ASP.Net MVC? • A new web project type for ASP.NET. • It’s not a replacement for webforms, it’s a new option [flavor] • No more “postbacks” and “ViewStates” • Built on top of ASP.NET 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]
  9. 9. ASP.Net MVC Overview ASP.Net MVC Project Structure App_Data folder is the physical store for data. Content folder keeps files such as scripts, CSS, images, etc Controllers 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.
  10. 10. ASP.Net MVC Overview ASP.Net MVC Execution Process Create MVC Perform Request request routing handler Create Execute Invoke action controller controller Render View
  11. 11. DEMO • Create New ASP.Net MVC Project • Investigate ASP.Net MVC Structure
  12. 12. Routing MVC +R R => the hidden character
  13. 13. Routing • Maps incoming URLs to the application and routes them to the right Controller’s Action method to execute them Default Routing /Products /Products/Categories /Products/Detail/34
  14. 14. Routing To customize routing rules
  15. 15. Model [M]
  16. 16. 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]
  17. 17. Controller [C] Controlling the flow of application execution
  18. 18. Controller  Controller is a class consisting of a set of action methods.  Responsible for o Handling user interaction o Working with the models o Selecting a view to render o Sending model and information to a particular view o Making decisions for security, UI, redirects ... etc.  The ASP.Net MVC requires the names of all controllers to end with the suffix quot;Controller“ e.g. HomeController, LoginController, or ProductController
  19. 19. Controller Controller Actions An action is a method on a controller that gets called when you enter a particular URL in your browser address bar. Actions Rules: 1) Must be a public method 2) Cannot be a static method 3) Cannot be an extension method 4) Cannot be a constructor or property 5) Cannot have generic type. 6) Cannot contain ref or out parameters.
  20. 20. Controller Controller Actions A controller action always returns an ActionResult 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]. 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
  21. 21. View [V]
  22. 22. 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
  23. 23. View HTML Helpers An HTML Helper is just a method that returns a string. e.g. • Html.ActionLink() • Html.ListBox() • Html.BeginForm() • Html.Password() • Html.CheckBox() • Html.RadioButton() • Html.DropDownList() • Html.TextArea() • Html.EndForm() • Html.TextBox() • Html.Hidden()
  24. 24. References •Official website •Source Code •Blogs o Scott Guthrie o Rob Conrey o MVC Store Front o Phil Haack o Scott Hanselman o Stephen Walthler
  25. 25. Ahmad R. ABU ARJA Senior Software Developer email: Thank you