A day with The Gu! MVC 2, VS2010 and ASP.NET v4.0 September 29, 2009Posted by codinglifestyle in ASP.NET, C#, jQuery, linq, Visual Studio 2010.
Tags: ASP.NET v4.0, beta, MVC, VS2010
Yesterday I went to Dublin to attend a talk by Scott Guthrie. I knew from reputation Scott was a good speaker so it was great to see him in action. I think most of the Microsoft development world is familiar with Scott’s blog. I’ve exchanged emails with him in the past and he has always done a great job following up. He is a very down to earth guy, very at ease at the podium, and very comfortable the material.
We started the talk with a beginner’s look at MVC 2 and then looked at .NET v4 and VS 2010. Some of this information was a recap of TechEd (see my earlier post), but there was plenty of new information which I’ll recap here.
Scott’s talk was about some of the improvements of the next version of MVC which will be baked in to VS2010. But thankfully, he covered the whole concept in a very demonstration-oriented way. He built upon each concept in a way that left me with a good grasp of the basics.
First, he reiterated that webforms is not going away. MVC is just an alternative presentation layer built upon the same core .NET libraries we know and love. Because there is no designer, no .NET controls, and no code behind (as such) you are much more in control of the generated HTML.
What MVC offers is that control, URL mapping, js integration, and testability. If you’ve ever worked on a messy web app and wished for more structure MVC may be for you. It offers a clean separation of your data layer (model), your html (view), and your business logic (controller).
Right, enough of this verbose carrying-on, time for bullet points!
· MVC 1 was an extra for VS2008 built on ASP.NET v3.5. MVC2 will be baked in to VS2010 and built on ASP.NET v4.0. It will be backwards compatible with MVC1 apps so upgrades should be a snap.
o URL Mapping – this is not just a cool feature but fundamental to MVC
§ http://localhost/galway maps to a controller class called galway
· .index is the default action method
§ http://localhost/galway/hooker maps to an action method inside controller Galway
§ http://localhost/galway/hooker/beer maps to the action method hooker and passes the string parameter “beer”. Note this is an alternative to query string parameters.
· These parameters can be strongly typed to string, int, even classes
§ Routing rules go in to gloal.asax.cs
· Operates like an HTTPHandler but is baked in to ASP.NET
· Order routing rules as you see fit. One falls through to another and ultimately to a default
· Can use regular expressions and constraints in your rules
o We can start playing with a controller without a View or Model and directly return some html from controller (think webservice)
o Controller action methods can return an action result type to return a View, redirect, etc.
o To communicate with View we can
§ store information in a ViewData[“key”] dictionary to pass to View
§ store information in a Model and pass this class to View
o Action Filters decorator attributes can be specified on the controller class or an action method to specify which roles / authorization required to use
o Tip: Use a service layer to keep direct data layer queries out of controller
o Offers separation of UI from business logic and just renders the UI
o Remember, no designer or ASP.NET controls. Just you, html, and <%inline code%>.
o HTML. Helper with many built-in templates to generate common controls like checkboxes and textboxes with validation
§ Create your own View templates to have custom scaffolding like a table for a DB list
o Html.EditorFor gives Linq type intellisense to meaning we aren’t binding to a “string” in our model
§ Smart in that Booleans render as checkboxes, etc.
§ EditorTemplates can be used to custom render anything can be shared across entire site or used for just one View
o Html.DisplayFor gives read-only view of data
o A data entity with logic.
§ Can be LinetoSQL, ADO.NET, your own entity class, whatever
o Can decorate properties with attributes to specify common validators
§ Required, range, etc.
§ Very powerful, dynamic, should greatly ease pain of validating form data
§ Automatically adds a CSS class you can customize to get a red background, whatever
§ Can have server and client side validation
· Client side requires an extra js plug-in but worked seamlessly in demo
· Unit testing is crucial component of MVC and a test project is automatically created for you with every MVC website
o Use AAA method
· Create a controller
· Get the result of (controller.Index(0) as ViewResult)
· Assert if result.IsNotNull
o Dependency injection
§ In the constructor pass DB service layer or fake data. Use an interface for flexibility.
· Beta 2 out shortly
· IDE improvements
o Ctrl-, – quick nav via types
o Highlight all references
o Tip: Download CodeRush Xpress for these features in VS2008)
· Better intellisense support
o camel case (i.e. DB matches DataBind)
o Matching (i.e. bind matches DataBind)
o Consume first mode for TDD (test driven development)
§ Ctrl + Alt + Space to toggle
§ XML documentation (place under function()) for better intellisense for your own libraries
· Debug History and dumping a crash covered again (see previous post)
· .NET 4 is a new CLR unlike 3.0 and 3.5
o In IIS you will see v4.0 as a selectable framework
· Upgrading to VS2010 hopefully just changes solution file (like VS2005 > VS2008) so painless enough to upgrade
· Multi-target support from .NET v2.0 on up
· Lots of project templates including a new online template gallery (web starter kits?)
· Controls to have ClientIDMode property
o Static – is what it is. Call it “bob” and you are guaranteed to get document.getElementByid(“bob”)
o Predictable – new default… no more ctrl001_ prefixing
o Auto – current
· CSS rendering support
o Big upgrades including alternatives to tables for .NET controls
· ViewState – off by default. Force developers to think when we really need it.
· URL routing like MVC for WebForms (connotical)
· SEO (Search Engine Optimization)
o Page.Description and Page.Keywords to generate <meta> tags
§ Idea: Place in master page, tie-in to DB, allow client to change as required
o New SEO plug-in for II7 will crawl site and indentify issues that reduce search relevancy
§ Can increase search traffic 30-40%
· ScriptManager support CDN allowing you to specify URL for AJAX and jQuery direct from http://ajax.microsoft.com. Will actually phantom redirect to very local source but browser histories across many site will use standard Microsoft url meaning high probability of being cached
· New controls
o QueryExtender search control – search a grid
o Chart control
· Validation like MVC for GridView, FormView, ListView
o Auto reflect on class for validation decorator attributes and dynamically render validators with client and server-side validation
· Output/object cache providers (aka customizable I’m sure)
· Pre-start application
o Keep your application up, cached, and ready vs. IIS default behavior which shuts down when not in use
· Performance monitoring
· <%: Html encoded string %>
· Deployment (see previous post)