Asp.net mvc

2,249 views

Published on

ASP.NET MVC presentation beginner to intermediate level

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,249
On SlideShare
0
From Embeds
0
Number of Embeds
62
Actions
Shares
0
Downloads
59
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Asp.net mvc

  1. 1. ASP.NET MVC CRUSH COURSE<br />ERDEM ERGİN<br />
  2. 2. MVC Pattern – What is it?<br />MVC - Acronym for Model/View/Controller<br />Invented by TrygveReenskaug[1973 - 1978]<br />Originally used as an architectural pattern for GUIs.<br />The first implementation of MVC was as part of Smalltalk-80 <br />class library. <br />
  3. 3. MVC Pattern – How does it work?<br />The Controller asks the Model for data<br />The request hits the controller<br />Model<br />2<br />1<br />3<br />Controller<br />User<br />The Model gives the data back to the Controller<br />The controller formats the data and passes them to the View<br />View<br />4<br />5<br />The view renders the HTML that needs to be sent to the client<br />3<br />
  4. 4. MVC Pattern – Who uses it?<br />.NET has Spring.NET, MonoRail<br />Java has Swing, Struts, Grails and others<br />Perl has Catalyst, Gantry, Jifty and others<br />PHP has Zend, Zoop, Agavi and others<br />Python has Django, Gluon, Pylon and others<br />Ruby has Ruby on Rails<br />Apple has cocoa and cocoa touch for objective-c [Mac, iPhone]<br />
  5. 5. MVC FrameworkHistory<br />Based on Python<br />1970S<br />….<br />2003 RoR<br />{ ASP.NET MVC }<br />2009.3 MVC1 <br />2010.4 MVC2<br />
  6. 6. ASP.NET MVC<br /><ul><li>It’s not a replacement for asp.net webforms, it’s a new option [flavor]
  7. 7. No more “postbacks” and “ViewStates”
  8. 8. Built on top of ASP.NET</li></ul>Advantages<br /><ul><li>Clean URLs and HTML
  9. 9. More control over your html and JavaScript
  10. 10. Friendly URLS
  11. 11. More easily Testable Framework.
  12. 12. All ASP.Net providers still work [Membership, Session, Caching, etc]
  13. 13. Extensible and Pluggable [Support replacing any component]</li></li></ul><li>ASP.NET MVC Framework Structure<br />ASP.NET Abstractions<br />(System.Web.Abstractions.dll)<br />ASP.NET Routing<br />(System.Web.Routing.dll)<br />ASP.NET MVC<br />(System.Web.Mvc.dll)<br />
  14. 14. ASP.NET MVC Execution Process<br />
  15. 15. DEMO<br />Create New ASP.NET MVC Project<br />Investigate ASP.NET MVC Structure<br />
  16. 16. Routing<br />MVC+R<br />R => the hidden character<br />
  17. 17. ASP.NET MVC Routing<br />URL Patterns<br />Setting Default Values for URL Parameters<br />Adding Constraints to Routes<br />Scenarios When Routing Is Not Applied<br />How URLs Are Matched to Routes<br />Accessing URL Parameters in a Routed Page<br />ASP.NET Routing and Security<br />ASP.NET Routing versus URL Rewriting<br />
  18. 18. Model [M]<br />Validation<br />
  19. 19. Controller [C]<br />Controlling the flow of application execution<br />
  20. 20. ASP.NET MVC Controller<br />Controller is a class consisting of a set of action methods.<br /> Responsible for <br />Handling user interaction<br />Working with the models<br />Selecting a view to render<br />Sending model and information to a particular view<br />Making decisions for security, UI, redirects ... etc.<br />The ASP.Net MVC requires the names of all controllers to end with the suffix "Controller“ e.g. HomeController, LoginController<br />
  21. 21. ASP.NET MVC Controller<br />A controller action always returns an ActionResult<br />What if I return an object ??<br />Action return ActionResult which can be<br /><ul><li>ViewResult => HTML and markup.
  22. 22. EmptyResult=> no result.
  23. 23. RedirectResult => redirection to a new URL.
  24. 24. JsonResult => json result that can be used in an AJAX application.
  25. 25. JavaScriptResult => JavaScript script.
  26. 26. ContentResult => text result [and wraps any non ActionResult return Type].
  27. 27. FileContentResult => downloadable file [with the binary content].
  28. 28. FilePathResult => downloadable file [with a path].
  29. 29. FileStreamResult => a downloadable file [with a file stream]. </li></ul>Note: all public methods of a controller class consideredas action methods, if you don’t want a public method to be an action, mark it with [NonAction()] attribute<br />
  30. 30. ASP.NET MVC Controller Filter<br />
  31. 31. View [V]<br />
  32. 32. ASP.NET MVC View<br />Responsible for presentation, look & feel, formatting, sorting … etc. <br />Interacts with model but shouldn’t make decisions <br />No logic should go there<br />Code Behind exists but not recommended<br />No View State, No Server Controls<br />HTML Helpers<br />Takes the view data from the controller<br />Can be strong typed <br />Can be extended by implementing View Engines<br />Always build your views !!!<br />
  33. 33. An HTML Helper is just a method that returns a string.<br />• Html.ActionLink()<br />• Html.BeginForm()<br />• Html.CheckBox()<br />• Html.DropDownList()<br />• Html.EndForm()<br />• Html.Hidden()<br />• Html.ListBox()<br />• Html.Password()<br />• Html.RadioButton()<br />• Html.TextArea()<br />• Html.TextBox()<br />e.g.<br />ASP.NET MVC View<br />
  34. 34. Extensibility<br />
  35. 35. The default ASP.NET MVC Pipeline<br />21<br />
  36. 36. The default components<br />URL Routing:<br />Parses the URL, andinstantiate the MvcHandler<br />Controller Factory<br />Takes URL parameters, create controller via reflectionbased on Controllername<br />Action Invoker<br />Invokes the actionbased on thename, with the filtersbefore and after<br />ViewEngine<br />WebFormsviewengine<br />Template<br />Renders a TextBoxalmostforeverything<br />HtmlHelper<br />Has a bunch of standardmethods<br />
  37. 37. Routingextensibility<br />RouteConstraint<br />Validatesrouteparameterswith code<br />RouteHandler<br />Defineshow the requestmustbehandled<br />
  38. 38. Controller Extensibility<br />ControllerFactory<br />ResponsibleforcreatingControllers<br />ActionInvoker<br />Invokesanaction, basedonly on itsname<br />ActionMethodSelectorAttribute<br />Helps the actioninvoker decide whichactiontoinvokewhen the nameisnotunique<br />Controller<br />The base classforevery controller<br />ActionResult<br />Decideshowtosend the output to the user<br />
  39. 39. ActionFilterExtensibility<br />AuthorizationFilter<br />Makessure the currentrequestisallowed<br />ActionFilters<br />Executedbefore or after the actionexecutes<br />ResultFilters<br />Executedbefore or afteranactionresultexecutes<br />
  40. 40. Binding/ValidationExtensibility<br />ModelBinder<br />Populates the actionmethodparametersfrom the request<br />ModelValidator Provider<br />Retrieves the validationrules<br />Server-sideValidationRules<br />The actual server-side validationrule<br />
  41. 41. ViewExtensibility<br />ViewEngine<br />The service thattransforms in HTML the data for the user<br />HtmlHelpers<br />Utility functionsthathideaway the generation of some HTML markup or JavaScript code<br />Client-sideValidationRules<br />Client-sidevalidationrules<br />ModelMetadata Provider<br />Retrieves the metadataneededfor the templatedhelpers<br />Custom Templates<br />Renders the html toedit/display specifictypes<br />
  42. 42. The Mostimportantextensibilitypoints<br />
  43. 43. Custom T4 Templates<br /><ul><li>WHAT: Generates View and Controller files </li></ul>DEFAULT: <vsdir>Common7IDEItemTemplatesCSharpWebMVC 2CodeTemplates<br />WHY: Add your own to generate Views and Controllers based on your needs<br />CopyCodeTemplates folder in you solution<br />Demo<br />
  44. 44. CustomTemplates<br /><ul><li>WHAT: Renders the html toedit/display specifictypes</li></ul>DEFAULT: Everythingis a label or a textbox<br />WHY: Add your own to customize specific data-types<br />Add PartialViews in:<br />/Views/Shared/DisplayTemplates<br />/Views/Shared/EditorTemplates<br />Demo<br />
  45. 45. Server-sideValidationRules<br />WHAT: Definehow a propertyisvalidated in the server side validation<br />DEFAULT: Required, Length, wrong type<br />WHY: Add your own rules<br />Write a new ValidationAttribute<br />Implement the IsValid method<br />Apply attribute to your model<br />Demo<br />
  46. 46. Client-sideValidationRules<br />WHAT: Definehow a propertyisvalidated in the client-side validation<br />DEFAULT: Client-sidecounterparts of the default server-side validators<br />WHY: Addyourownvalidators<br />
  47. 47. Client-sideValidationRules<br />First make a server-side validator<br />Makevalidationlogic in JavaScript<br />Writeadaptertopushvalidationrulesto client-side<br />Registervalidationfunction via JS<br />Registeradapter in Global.asax<br />Demo<br />
  48. 48. Base Controller<br />WHAT: The base class for every Controller<br />DEFAULT: Default implementation of helper methods<br />WHY: Extend if you want to enforce you own conventions<br />Override Controller<br />YourcontrollersoverridefromBaseControllerinsteadof Controller<br />Demo<br />
  49. 49. HtmlHelpers<br />WHAT: Utility functions that hide away the generation of HTML markup or JavaScript code<br />DEFAULT: Html.TextBox, Html.Encode, Html.Partial, …<br />WHY: “If there is an if, write an Helper”<br />Add new methods as extension methods<br />Demo<br />

×