350 likes | 530 Views
Introducing ASP.NET MVC 3. Ori Calvo, 2010 Hi-Tech College Ori.calvo@gmail.com. “If people want to have maximum reach across * all * devices then HTML will provide the broadest reach” Scott Guthrie, Corporate Vice President , Microsoft. Objectives. ASP.NET MVC Basics
E N D
Introducing ASP.NET MVC 3 Ori Calvo, 2010 Hi-Tech College Ori.calvo@gmail.com
“If people want to have maximum reach across *all* devices then HTML will provide the broadest reach” Scott Guthrie, Corporate Vice President, Microsoft
Objectives • ASP.NET MVC Basics • What’s new in ASP.NET MVC 3 • Summary
Prerequisites • ASP.NET Developer • Good understanding of HTML and HTTP • Good understanding of C# 3.0 Language • No ASP.NET MVC Experience is required
Quick Demo • Create new ASP.NET MVC Project • Create new controller • Create new View
Classical ASP.NET • Huge shift at that time • Object oriented approach • Compiled web pages • Stateful UI • Event Driven • Drag and Drop Designer • Windows Forms Developing Experience • Is here to stay !!!
What’s wrong with it? • Is “Windows Forms” approach suited? • Limited control over HTML • Encourages mixing of presentation and logic • Hard to test • ViewState • Complex page life cycle • Hard to integrate with other libraries
ASP.NET MVC • Separation of concerns • Tight control over HTML • Designed to be testable • Extensible routing system • Better integration with 3rd party libraries • Open source • Built on top of ASP.NET platform • Language innovations
Software Requirements • .NET Framework 4.0 • Visual Studio 2010 • Visual Web Developer 2010 • Can be installed into the bin directory • In case the Web Host does not support it directly • Installation can be automated by Web Platform Installer • Additional components can be installed using NuGet
MVC Ingredients • Routing System • Controller • Action • Filter • View • Model • Model State • View Bag
Demo – Real life controller • GroupController • Index View • Click a group • Back to parent
Demo – Extending Views • Partial View - Menu • Layouts - Header and footer • Sections – Add “scripts” section
Demo – Submitting Data • Create new group • Method selector • Automatic model binding • Validation • DataAnnotations • Edit a group • TryUpdateModel • jQuery to set focus
Demo – Controlling Access • Authorize filter • FormsAuthentication • LoginDetails as ViewModel
Demo – Routing System • Simplifying URLs • Implement “CMS like” URLs
What’s new? • ASP.NET MVC 3 • NuGet 1.1 • IIS Express 7.5 • SQL Server Compact Edition 4.0 • Web Deploy and Web Farm Framework 2.0 • Orchard 1.0 • Web Matrix 1.0 • Entity Framework Code First (CTP5)
Razor • Just another View Engine • Better integration of C# and HTML • Is focused around the @ character • No need to set the end of code • Is testable • Is extendible • Follows the same compilation model as ASPX • Can be compiled at design time
Razor syntax • Use @ to move between code and HTML • Use @() to specify long expression • Use @{} to run arbitrary code which doesn’t writes into the response stream • Use @: inside code region to move back to HTML • Can also use <text> • Use @* and *@ for comments • Use @functions to add some functions • Use @inherits to specify different base class
Demo - Razor • Resolve ambiguities • Create a unit test • Write inline template
NuGet • Consider following scenario • You want to install a library named X • X has a dependency on Y • You also want to install library named Z • Z has a dependency on Y • Need to think about: • Where can you get latest of all libraries? • What are the dependencies of each library? • Is it ok to use the latest release for Y? • If not, what happens if Z depends on a different version of Y than X?
NuGet • A library package manager • Deals with packages rather than assemblies • An extension to Visual Studio 2010 • Not just for ASP.NET MVC projects • Packages come from a well known repository • Automatically handling of version conflicts and updates • Once package is imported NuGet stays out of the way • No integration with TFS
NuGet Algorithm • Never installs assemblies at machine level • Uses bin deploy • Prefers unification over “side by side” • Always picks the lowest version of a dependency that fits in the range • Always move up to the highest build/revision • Can use the Add-BindingRedirect power shell command
Demo - NuGet • Install T4MVC • Install NHibernate • Handle conflicts • Get a list of updates available • Create a local repository • Give a look at http://nuget.org
IIS Express • Quick installation (less than 10MB) • Better support for IIS feature set • SSL • Integrated Mode • URL Re-Writing • Same configuration schema • Does not require administrator privileges • Doesn’t run as a service • Can co-exist with full IIS7.x and Cassini • Works on Windows XP SP3 and later • Supports multiple users on the same machine • Supports WCF but only over HTTP • Integrated into Visual Studio 2010 SP1 beta
Demo - IIS Express • Changing VS to work with IIS Express • Change VS default • Execute under Integrated Mode • Change application pool using AppCmd.exe
Demo – EF Code First • Use DbContext • Change the database name • Use SQL CE • Dispose the context • Use validation
What’s new in ASP.NET MVC 3? • Unobtrusive JavaScript • Posting JSON objects • Class level model validation • Partial page output caching • Better Dependency Injection • ViewBag • Global Filters • Sessionless Controller
ASP.NET MVC vs. ASP.NET Web Forms • Both based on ASP.NET infrastructure • Same objectives, different ways • Both server centric • How sophisticated the UI need to be? • Are you planning for automation coverage? • What is your team developing background? • Can mix both in the same project • You can use ASP.NET Server Controls inside views (don’t tell anybody …)
Summary • A different way to develop web applications • Razor Syntax – Clear & easier to maintain • NuGet – Integrate 3rd party libraries • IIS Express – IIS for the developer • Better Validation – Based on jQuery and Unobtrusive JavaScript principles • Many small enhancements
TryUpdateModel Questions ?
Read more • Course 4282 at Hi-Tech College (Next Week!!!) • http://weblogs.asp.net/scottgu/ • http://www.hanselman.com/blog/ • http://www.asp.net/mvc • http://jquery.com/