mvc presentation by Nitin Sawant


Published on mvc presentation by Nitin Sawant

Published in: Software
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide mvc presentation by Nitin Sawant

  1. 1. Introduction To ASP.NET MVC 4 Nitin Sawant .NET developer
  2. 2. Webforms Pros • Rich Controls (grids, etc…) • RAD • Event Driven (like Winforms) • Stateful • Less Control • No built-in Separation of Concerns (SoC) • Complicated Page life-cycle • Not SEO friendly  URL  Big Viewstate at the beginning of the page Webforms Cons
  3. 3. MVC Pattern • What is MVC? • Why MVC? • Who uses MVC? …“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 - Acronym for Model-View-Controller  MVC is a architectural pattern  Invented by Trygve Reenskaug in 1978  Originally used as an architectural pattern for building GUIs. MVC Pattern What is it?
  5. 5. Why MVC? ASP.NET MVC Pros  More control over your html and JavaScript  Separation of Concerns (SoC)  SEO friendly  Test Driven Development (TDD)  Highly flexible & customizable  Not RAD  No rich controls ASP.NET MVC Cons
  6. 6. MVC Pattern Who uses it? • .NET has Spring.NET and 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 Execution Process • ASP.Net MVC 4 Project Structure
  8. 8. ASP.Net MVC Overview ASP.Net MVC Architecture M Model V View C Controller • Representation of domain data • Business Logic • Persistence mechanisms • User Interface • The representation of Model • An intermediary between Model and View • Application’s Brain (Handle user requests, bind Data, return views)
  9. 9. ASP.NET Dynamic Data ASP.NET WebForms ASP.NET MVC Presentation Runtime ASP.NET Core It’s not a replacement for webforms, it’s a new flavour of ASP.Net MVC Overview ASP.Net MVC?
  10. 10. ASP.Net MVC Overview ASP.Net MVC Execution Process
  11. 11. ASP.NET MVC 4 Project Structure 1. App_Data folder is the physical store for data. 2. Content folder keeps files such as scripts, CSS, images, etc 3. Controllers folder is the location for controllers. 4. Models stores classes that handle application domain logic. 5. Scripts folder is for script files. By default, this folder contains AJAX script files and the JQuery library. 6. Views is the recommended location for views. View files use .cshtml extension.
  12. 12. Routing R => the hidden characterMVC +R 1. Maps incoming URLs to the application and routes them to the right Controller’s Action method to execute them 2. Happens in RegisterRoutes function in the global.asax file
  13. 13. Routing Don’t settle for… /ProductsDetails.aspx?CategoryID=123 When you can easily have… /Product/Details/123/ Or whatever else makes sense…
  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] Controls the flow of application execution
  18. 18.  Controller is a class containing 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 "Controller“ e.g. HomeController, LoginController & ProductController Controller
  19. 19. 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. 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.
  20. 20. Each action returns 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 Controller Controller 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
  21. 21. View [V]
  22. 22. View • Responsible for presentation, look & feel, formatting, sorting … etc. • Interacts with model but doesn’t make any decisions • No logic should go there • No View State, No Server Controls • HTML Helper functions • Can be strongly typed • Takes the view data from the controller Tips: • @ is equivalent to response.write() • @{} is used to execute code • Use @Html.Encode() method to prevent script injection
  23. 23. View Engines ASPX Razor
  24. 24. View Engines ASPX Razor
  25. 25. • Html.ActionLink() • Html.BeginForm() • Html.CheckBox() • Html.DropDownList() • Html.EndForm() • Html.Hidden() An HTML Helper is just a method that returns a string. View HTML Helpers • Html.ListBox() • Html.Password() • Html.RadioButton() • Html.TextArea() • Html.TextBox() e.g.
  26. 26. 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
  27. 27. Nitin Sawant Software Engineer T h a n k y o u