1 / 266

MVC 4

MVC 4. Spindustry Training. Cont. MVC separates the user interface of an application into three main aspects: The Model The View The Controler. The Model.

tudor
Télécharger la présentation

MVC 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. MVC 4 Spindustry Training

  2. Cont. • MVC separates the user interface of an application into three main aspects: • The Model • The View • The Controler

  3. The Model • A set of classes that describes the data you’re working with as well as the business rules for how the data can be changed and manipulated

  4. The View • Defines how the application’s UI will be displayed

  5. The Controller • A set of classes that handles communication from the user, overall application flow, and application-specific logic.

  6. MVC as Applied to Web Frameworks • Models: • Represent domain objects that encapsulate: • data stored in a database • code to manipulate the data • code to enforce domain-specific business logic • Example: encapsulating Entity Framework

  7. Cont. • View: • Template to dynamically generate HTML • Controller: • Class that manages the relationship between the View and the Model • It responds to user input, talks to the model, and decides which view to render (if any).

  8. A Pattern • MVC is a pattern that can be applied to different frameworks • ASP.NET MVC • The MVC pattern has been applied to ASP.NET. This does not mean that it is the same as MVC running in other places.

  9. ASP.NET MVC 3 • 10 months after MVC 2 • The Razor view engine • Support for .NET 4 Data Annotations • Improved model validation • Greater control and flexibility with support for dependency resolution and global action filters • Better JavaScript support with unobtrusive JavaScript, jQuery Validation, and JSON binding • Use of NuGet to deliver software and manage dependencies throughout the platform

  10. Razor View Engine • Code-focused templating for HTML generation • Compact, expressive, and fluid • Not a new language • Easy to learn • Works with any text editor • IntelliSense • No XML-like heavy syntax

  11. ASP.NET MVC 4 • Features include: • ASP.NET Web API • Enhancements to the default project templates • Mobile project template using jQuery Mobile • Display Modes • Task Support for Asynchronous Controllers • Bundling and Minification

  12. ASP.NET Web API • Referred to as Web API • A framework that offers the ASP.NET MVC development style but is tailored to writing HTTP services. (service-oriented design) • Several MVC features have been adopted for HTTP Service domain: • Routing • Model Binding and Validation • Filters • Scaffolding • Easy Unit Testability

  13. Cont. • Web API also adds a few new concepts and features to the HTTP service development: • HTTP programming model • Action dispatching based on HTTP verbs • Content negotiation • Code-based configuration

  14. ASP.NET MVC 5 • http://www.asp.net/mvc/mvc5

  15. Enhancements to the default project templates

  16. Cont.

  17. Display Modes • A convention-based approach to allow selecting different views based on the browser making the request. • Index.cshtmlvsIndex.Mobile.cshtml • You can also register your own custom criteria. • Index.WinPhone.cshtml

  18. Bundling and Minification • Same as ASP.NET 4.5 • Works on scripts and CSS • Minifies the request via several techniques • Compresses the size of CSS via several techniques • You can create custom bundles to contain specific scripts and reference them with a single URL

  19. Cont.

  20. Included Open Source Libraries • Json.NET • Included in MVC 4 as a part of the Web API to support serializing data to JSON format allowing for: • Data contracts • Anonoymous types • Dynamic types • Dates • TimeSpans • Object reference presevation • Indenting • Camel casing • LINQ to JSON is also included along with automatic conversion from JSON to XML

  21. Cont. • DotNetOpenAuth: • Used to support OpenID and Oauth-based logins • Facebook • Microsoft • Google • Twitter • MVC 4 also provides an OAuthWebSecurity class

  22. App_Start

  23. Cont. • AuthConfig.cs • Used to configure security settings, including sites for OAuth login. • BundleConfig.cs • Used to register bundles used by the bundling and minification system. By default it inlcudes jQuery, jQueryUI, jQuery valiation, Modernizr, and default CSS references • FilterConfig.cs: • Used to register global MVC filters. By default the HandlerErrorAttribute is registered. You can put other filter registrations here.

  24. Cont. • Filters: • An action filter is an attribute that you can apply to a controller action -- or an entire controller -- that modifies the way in which the action is executed. The ASP.NET MVC framework includes several action filters: • OutputCache – This action filter caches the output of a controller action for a specified amount of time. • HandleError – This action filter handles errors raised when a controller action executes. • Authorize – This action filter enables you to restrict access to a particular user or role.

  25. Cont. • RouteConfig.cs • Contains routing • WebApiConfig.cs • Used to register Web API routes, as well as set any additional Web API configuration settings

  26. Open Source Release • May 2012 the ASP.NET Web Stack open source announcement marked the transition of ASP.NET MVC, ASP.NET Web Pages, and ASP.NET Web API from open source licensed code to fully open source projects. • All code changes and issue tracking for these projects is done in public code repositories, and these projects are allowed to accept community code contributions if the team agrees that the changes make sense. • http://aspnetwebstack.codeplex.com/SourceControl/list/changesets

  27. Installing MVC 4 • Visual Studio 2012 (included) • Can be installed on: • Visual Studio 2010 SP1 • Visual Web Developer 2010 Express SP1 • Can be installed side-by-side previous versions of MVC

  28. Templates

  29. Cont. • Internet Application: • Contains the beginnings of an MVC web application, including basic account management functions that use ASP.NET Membership. • Intranet Application: • Added in ASP.NET MVC 3 tools update. Similar to the Internet Application template, but the account management functions use Windows accounts rather than ASP.NET Membership. • Basic Template: • Minimal. Has basic folders, CSS, and MVC application infrastructure in place, but no more. You need to do work in order to get the application to run. For experienced MVC developers that don’t want the predefined items in the project.

  30. Cont. • Empty: • Only has the assemblies and the basic folder structure in place • Mobile Application: • Preconfigured with jQuery Mobile. • Includes mobile visual themes, a touch-optimized UI, and support for Ajax navigation • Web API template: • Similar to the Internet Application template but is streamlined for Web API development

  31. View Engines • ASPX • Razor

  32. Testing • Just check the box! • You can add other Test Frameworks like NUnit, MbUnit, or xUnit if you want to. • http://msdn.microsoft.com/en-us/library/dd381614.aspx

  33. MVC Application Structure • Controllers – Controller classes that handle URL request go here (MVC 4, controller classes can go anywhere) • Models – Classes that represent and manipulate data and business objects go here • Views – UI template files that are responsible for rendering output go here • Scripts – JavaScript files and scripts go here • Images – images used on your site go here

  34. Cont. • Content – CSS and other site content, other than images and scripts, goes here • Filters – Filter code goes here • App_Data – Store data files that you read and write to, here • App_Start – Configuration code for features like Routing, Bundling and Web API goes here

  35. ASP.NET MVC does not require this Directory Structure.

  36. Conventions • “Convention over configuration” • ASP.NET MVC is heavily convention-based • Uses Directory-naming structure when resolving view templates. • Allows you to omit the location path when referencing views from within a Controller class.

  37. Cont. • Each controller’s class name ends with Controller • LegalController, LegalServicesController, HomeController • One Views directory for all the views of your application • Views that controllers use live in a subdirectory of the Views main directory and are named according to the controller name(minus the Controller suffix) • Views for the LegalController would live in /Views/Legal • Reusable UI elements live in a similar structure, but in a Shared directory in the Views folder

  38. 2. Controllers

  39. Controller Basics • Model View Controller: • 1st Controller basics, then the other stuff. • Have a look at the HomeController • Responsible for deciding what will happen when you browse to the homepage of the website.

  40. The Controller’s Role • Responsible for responding to user input, making changes to the model in response to user input if needed. • Controllers are concerned with the flow of the application, working with data that comes in and providing data going out to the relevant view.

  41. Cont. • The URL tells the routing mechanism (later chapter) which controllerclass to instantiate and which action method to call, and supplies the required arguments to that method. • The controller’s method then decides which view to use, and that view then renders the HTML. • The controller’s method could return something other than a view.

  42. Cont. • There is a relationship between the URL and the METHOD on a controller class. Not a relationship between the URL and a FILE on the web server. • MVC serves up the results of method calls, not dynamically generated pages. • (More about routing in later chapter)

  43. IController

  44. ControllerBase

  45. Controller Class

  46. Controller Actions • Add additional methods for additional scenarios. • These methods are called Controller Actions or Action Methods • They respond to URL request, perform the appropriate actions, and return a response back to the browser or user that invoked the URL

  47. MVC Request processing pipeline

More Related