290 likes | 439 Views
PDC09-FT22. Microsoft ASP.NET MVC 2: The New Stuff. Stephen Walther Senior Program Manager Microsoft Corporation. ASP.NET MVC 1.0. Everything starts so simple, but then…. ASP.NET MVC 1.0. ASP.NET MVC 1.0. Enables you to build applications that can be maintained over the long term:
E N D
PDC09-FT22 Microsoft ASP.NET MVC 2: The New Stuff Stephen Walther Senior Program Manager Microsoft Corporation
ASP.NET MVC 1.0 Everything starts so simple, but then…
ASP.NET MVC 1.0 Enables you to build applications that can be maintained over the long term: • Supports a clear separation of concerns • Supports testability • Supports “close to the metal” programming experience
What’s New in MVC 2? • Better Separation of Concerns (Maintainability) • Html.RenderAction() • Areas • Easier Validation (Maintainability/Productivity) • Data Annotations • Client Validation • Helper Improvements (Maintainability/Productivity) • Strongly-Typed Helpers • Templated Helpers
ASP.NET MVC 2 Visual Studio 2010 Included Visual Studio 2008 (Service Pack 1) Download Both versions built against .NET 3.5
RenderAction(Composability) <% Html.RenderAction() %> • Menus • Banner Ads • Shopping Carts Any UI that appears in multiple pages and requires business logic
RenderAction(Maintainability) • Html.RenderPartial() Renders UI • Html.RenderAction() Invokes controller action that renders UI
Using RenderAction demo
Areas (Maintainability) Areas Enables you to maintain a clear separation of functionality in a single project AreaRegistration.RegisterAllAreas();
Using Areas demo
Validation (Maintainability/Productivity) Client-Side Validation Server-Side Validation Define Validation Model Class
Validation (Maintainability/Productivity) • Model Validation Providers • Data Annotation (Default) • Enterprise Library • XML Anything you want
Validation (Maintainability/Productivity) Data Annotation Validators • [Required] • [Range] • [RegularExpression] • [StringLength] Custom Validator
Validation (Maintainability/Productivity) • public class Customer • { • [Required] • [DisplayName("First Name")] • public string FirstName { get; set; } • [Required] • [RegularExpression(@"\S+@\S+\.\S+")] • public string Email { get; set; } • }
Validation (Maintainability/Productivity) Client-Side Validation <script src="../../Scripts/MicrosoftAjax.debug.js" type="text/javascript"></script> <script src="../../Scripts/MicrosoftMvcAjax.debug.js" type="text/javascript"></script> <% Html.EnableClientValidation(); %>
Strongly-Typed Helpers (Maintainability) • Html.TextBoxFor() • Html.TextAreaFor() • Html.ValidationMessageFor() …
Templated Helpers (Productivity) Display Helper Methods • Html.Display() • Html.DisplayFor() • Html.DisplayForModel() Edit Helper Methods • Html.Editor() • Html.EditorFor() • Html.EditorForModel()
Templated Helpers (Productivity) Specify Template: • By Type: DateTime.ascx • By Name: Html.DisplayForModel(“MyTemplate.ascx”); • By UIHint [UIHint("MyPropertyTemplate")] public string Title { get; set; }
Templated Helpers (Productivity) Fallback: • Employee • Person • Object
Templated Helpers demo
Other New Features • Security • HTML Encoding Syntax • <%:Model.NewPostByEvilPerson%> • Works with ASP.NET 4 • JsonResult • Performance Improvements • Asynchronous Controller Actions • Convenience Improvements • Default Parameters for Controller Actions • HttpPost, HttpGet, RequiresHttps • Value Providers
Value Providers Source for Model Binder data: • FormValueProvider • RouteDataValueProvider • QueryStringValueProvider • HttpFileCollectionValueProvider and custom…
Using a JsonValueProvider with ASP.NET MVC 2 and the Microsoft Ajax Library demo
Why ASP.NET MVC 2 is Great! • Separation of Concerns (Maintainability) • RenderAction() • Areas • Validation (Maintainability/Productivity) • Data Annotations Support • Client Validation • Helper Improvements (Maintainability/Productivity) • Strongly-Typed Helpers • Templated Helpers