The document provides an overview of the MVC pattern and ASP.NET MVC framework. It discusses that MVC separates an application into three components: the model, the view, and the controller. It then describes how ASP.NET MVC uses the MVC pattern and some of its key aspects, including controllers, views, routing, and extensibility points. The document aims to provide a high-level introduction to ASP.NET MVC.
2. MVC Pattern – What is it? MVC - Acronym for Model/View/Controller Invented by TrygveReenskaug[1973 - 1978] Originally used as an architectural pattern for GUIs. The first implementation of MVC was as part of Smalltalk-80 class library.
3. MVC Pattern – How does it work? The Controller asks the Model for data The request hits the controller Model 2 1 3 Controller User The Model gives the data back to the Controller The controller formats the data and passes them to the View View 4 5 The view renders the HTML that needs to be sent to the client 3
4. 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]
17. ASP.NET MVC Routing URL Patterns Setting Default Values for URL Parameters Adding Constraints to Routes Scenarios When Routing Is Not Applied How URLs Are Matched to Routes Accessing URL Parameters in a Routed Page ASP.NET Routing and Security ASP.NET Routing versus URL Rewriting
20. ASP.NET MVC Controller Controller is a class consisting of a set of action methods. Responsible for Handling user interaction Working with the models Selecting a view to render Sending model and information to a particular view 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
29. FileStreamResult => a downloadable file [with a file stream]. 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
32. 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 Always build your views !!!
33. An HTML Helper is just a method that returns a string. • Html.ActionLink() • Html.BeginForm() • Html.CheckBox() • Html.DropDownList() • Html.EndForm() • Html.Hidden() • Html.ListBox() • Html.Password() • Html.RadioButton() • Html.TextArea() • Html.TextBox() e.g. ASP.NET MVC View
36. The default components URL Routing: Parses the URL, andinstantiate the MvcHandler Controller Factory Takes URL parameters, create controller via reflectionbased on Controllername Action Invoker Invokes the actionbased on thename, with the filtersbefore and after ViewEngine WebFormsviewengine Template Renders a TextBoxalmostforeverything HtmlHelper Has a bunch of standardmethods
38. Controller Extensibility ControllerFactory ResponsibleforcreatingControllers ActionInvoker Invokesanaction, basedonly on itsname ActionMethodSelectorAttribute Helps the actioninvoker decide whichactiontoinvokewhen the nameisnotunique Controller The base classforevery controller ActionResult Decideshowtosend the output to the user
40. Binding/ValidationExtensibility ModelBinder Populates the actionmethodparametersfrom the request ModelValidator Provider Retrieves the validationrules Server-sideValidationRules The actual server-side validationrule
41. ViewExtensibility ViewEngine The service thattransforms in HTML the data for the user HtmlHelpers Utility functionsthathideaway the generation of some HTML markup or JavaScript code Client-sideValidationRules Client-sidevalidationrules ModelMetadata Provider Retrieves the metadataneededfor the templatedhelpers Custom Templates Renders the html toedit/display specifictypes
45. Server-sideValidationRules WHAT: Definehow a propertyisvalidated in the server side validation DEFAULT: Required, Length, wrong type WHY: Add your own rules Write a new ValidationAttribute Implement the IsValid method Apply attribute to your model Demo
46. Client-sideValidationRules WHAT: Definehow a propertyisvalidated in the client-side validation DEFAULT: Client-sidecounterparts of the default server-side validators WHY: Addyourownvalidators
47. Client-sideValidationRules First make a server-side validator Makevalidationlogic in JavaScript Writeadaptertopushvalidationrulesto client-side Registervalidationfunction via JS Registeradapter in Global.asax Demo
48. Base Controller WHAT: The base class for every Controller DEFAULT: Default implementation of helper methods WHY: Extend if you want to enforce you own conventions Override Controller YourcontrollersoverridefromBaseControllerinsteadof Controller Demo
49. HtmlHelpers WHAT: Utility functions that hide away the generation of HTML markup or JavaScript code DEFAULT: Html.TextBox, Html.Encode, Html.Partial, … WHY: “If there is an if, write an Helper” Add new methods as extension methods Demo