# Monday, 05 October 2009

Episode 57

In this interview, Dr. David Truxall discusses the art of debugging and dives into WinDbg and other tools to debug production issues.

Monday, 05 October 2009 13:01:31 (GMT Daylight Time, UTC+01:00)
# Sunday, 04 October 2009

The ASP.NET MVC framework (which I will refer to as "MVC" in this article) encourages developers to work closer to actual rendered HTML than does the more traditional web forms ASP.NET framework. The web form framework abstracted away much of the HTML, allowing developers to create sophisticated web pages simply by dragging controls onto a design surface. In fact, with the web forms framework, it is sometimes possible for someone with no knowledge of HTML to build an entire web application.

But MVC’s view engine removes that abstraction, encouraging users to write more of their own HTML. By doing so, developers also get more control over what is rendered to the client.

Some web developers may be surprised to learn that most of the server controls they are used to dragging onto a design surface do not work in MVC. This is because ASP.NET server controls are self-contained objects that encapsulate all their functionality, including the C# or VB code they run. This is contrary to the way that MVC works. In the MVC framework, all business logic code is in either the model (if it applies to the data) or in the controller if it applies to routing or output.

An MVC view consists only of an ASPX page. By default it doesn't even contain a code-behind file.

Let’s analyze a typical MVC view. Like a web form, an MVC view contains a page directive at the top.

<%@ Page Title="Sports" Language="C#" 

The Title, Language and MasterPageFile attributes should be familiar to anyone who has developed in ASP.NET. The meanings of these attributes have not changed in MVC. The Inherits attribute is also used in web forms development, but in this case, we are inheriting from a ViewPage that contains our model.  The model represents the data we wish to render (I will write more about the MVC model in a future article.) By inheriting from a ViewPage, we provide the model data directly to the page and we can strongly type the keyword Model to whatever data type our model is.

Mixed in with the HTML of our page, we can output some server side data by enclosing it in <% %> tags.

If you wrote any web sites with classic ASP (the predecessor to ASP.NET), you probably used these tags. (I recently heard someone refer to them as "bee stingers").  If you place "=" in front of a value or variable, the view engine will output the value or contents of that variable. For example

<%=System.DateTime.Now %>

outputs the current date and time, like the following

10/2/2009 6:08:46 PM

We mentioned earlier that the Model keyword is strongly-typed. For example, If we inherit our ASPX from  System.Web.Mvc.ViewPage<TestMvc.Models.Customer>, then our model represents a Customer object and we can output a property of that model class.

Assuming that our Model is uses the following Customer class:

public class Customer
    public string FirstName { get; set; }
    public string LastName { get; set; }

, we can output the FirstName property with the following markup:

<%= Model.FirstName %> 

You probably already know that it is almost always a bad idea to output text directly for the screen without encoding it, as I did above. Failure to do so may leave your site open to scripting attacks. Fortunately MVC includes an Encode helper method to encode strings for safer output. That helper method (along with other helpers) is available via the Html property of the ViewPage from which our view inherits. We can call the helper method and encode the first name with the following markup.

<%= Html.Encode(Model.FirstName) %>

This outputs the FirstName property, but encodes it to prevent problems if the first name property somehow gets infected with some client-side script.

Other helper methods of the ViewPage give you the ability to begin and end an HTML form, to render an HTML textbox, to render a hidden field, to render an HTML listbox, and perform many other useful functions.

The code below will output a hyperlink with the text "Home" that links to the Index method in the Home controller.

<%= Html.ActionLink("Home", "Index", "Home")%>

Another way to use data in the view is to store it in ViewData. ViewData is a property of the view and contains a dictionary of name/value pairs. You can add a value to this dictionary in the controller with code like the following

ViewData["Greetings"] = "Hello dummy";

and display a ViewData element in the view with markup like the following

<%= Html.Encode(ViewData["Greetings"]) %>

Below is the full markup for a sample MVC view page

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVCDemoView.Models.Customer>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<%= Html.Encode(ViewData["Greetings"]) %>
<%= Html.Encode(Model.FirstName) %>
<%= Html.Encode(Model.LastName) %>    
<%= Html.ActionLink("Home", "Index", "Home")%>

Here is the output of the view page above

Hello dummy
David Giard

If you don’t like the view engine that ships with MVC, it is possible to replace it with your own. Some open source projects, such as Spark have already given users this ability.

Using the MVC view engine encourages developers to have more control over the output sent to the client and provides a greater separation of concerns for the application as a whole.

Download the code for this sample at MVCDemoView.zip (281.64 KB)

Sunday, 04 October 2009 20:38:29 (GMT Daylight Time, UTC+01:00)
# Saturday, 03 October 2009

I am months late producing this video. But now that it's finished, I want to show it off. Earlier this year, my son's 8th grade basketball team tied for the city championship. Here are highlights from the season.

Saturday, 03 October 2009 15:30:23 (GMT Daylight Time, UTC+01:00)
# Friday, 02 October 2009

One of the differences between ASP.NET MVC  and traditional ASP.NET web forms is the URL typed by the user.

In traditional ASP.NET, a URL points to a physical page on disc. Typically one can infer the location of that page by examining the URL. For example, the URL


likely points to a file named MyPage.aspx located in a folder named Folder1.

By contrast, a URL in an ASP.NET MVC application points to an action in a controller. For example, the URL


Tells the MVC framework to pass a parameter of "2" to an action method named Edit in a Customer controller class.

ASP.NET MVC (which I’ll call MVC going forward) uses routing to map a URL to a specific action. Routing is typically configured in the  Application_OnStart method of the global.asax file.  An MVC application contains a System.Web.Routing.RouteTable, which contains a collection of routes on which it can act. When you create a new MVC project, a default route is added to this collection by adding the following code in global.asax.

    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults

This code adds a route named "Default" to the route table. This route tells MVC to look for a URL structured similar to the following


When it encounters such a URL, it looks for a controller class with a name based on the first part of the URL. By convention, MVC looks for controllers in the "Controllers" folder and is named with the suffix "Controller". So if the first part of the URL is "Customer", MVC will look in the controllers folder for a class derived from System.Web.Mvc.Controller named "CustomerController".

The second part of the URL identifies an action. The action is a method within the controller that returns a  System.Web.Mvc.ActionResult. The ActionResult identifies the view used to render a response (more on that in a later article). So if the second part of the URL is "Edit", MVC will look for a method in the controller class named "Edit" that returns an ActionResult.

The third part of the above URL defines a parameter for the action, in this case a parameter named "id".

The following URL


causes MVC to search for a class named Controllers\CustomerController, to call the Edit method in this class and to pass the value 2 to the id parameter of the Edit method.

What if the URL does not contain all three parts? The Default route above specifies a default value for each of the parts. If the third part of the URL is omitted, no parameter is passed to the Action. For a URL with no id parameter, we will need to create an action method that does not expect a parameter. If the second part of the URL is omitted, the default action is "Index", so MVC will look for a method named "Index" in the controller class. If the first part of the URL is omitted, it defaults to "Home" so MVC will look for a Controller class named "HomeController" in the Controllers folder.

You can use this same syntax to add more routes to your application. Simply add more calls to routes.MapRoute() in Global.asax. MVC will match URLs to these routes in the order listed.

Notice that the RouteTable and RouteCollection are part of the System.Web.Routing namespace and not part of any MVC namespace. This should tell you that the routing engine is not restricted to MVC applications. It can be used in any ASP.NET application if you want cleaner URLs.

If you find a URL like /Customer/Edit/2 more asthetically pleasing than /Customers/EditCustomer.aspx?CustID=2, you should investigate the routing engine.

Friday, 02 October 2009 14:21:00 (GMT Daylight Time, UTC+01:00)
# Thursday, 01 October 2009

Microsoft first released ASP.NET almost eight years ago. At that time, they created a model they hoped would be familiar (and compelling) to Windows forms developers, particularly to those working in Visual Basic 6.

ASP.NET adopted a development paradigm that mimicked Windows forms. This programming model was referred to as “web forms” and it abstracted away much of the complexity of HTML and HTTP, easing the transition from Windows to the web for many developers. Using web forms, developers could drag controls, such as buttons and textboxes, onto a design surface and add code that ran automatically when a user interacted with a control – such as when a user clicks a button. Pages and controls had events, such as Load and Init that ran at specified times during a page’s creation. By default, each page had a code-behind file – class containing code that responded to page events. These concepts were familiar to Visual Basic developers and this helped to drive adoption of ASP.NET.

But there are some drawbacks to this model. Web forms abstracted away the details of HTML and HTTP, but this abstraction cost the developer control over the rendering of pages and controls. Some developers felt that the web forms framework got in the way of their web developing by hiding these basic constructs. Also, tying code to web form events made it difficult to test much of the application. Testing frameworks evolved that would mimic the behavior of a web server, just to integrate with automated unit testing frameworks.

Recently Microsoft released ASP.NET MVC - a new programming framework designed to build web applications with HTML and HTTP. This framework uses the Model-View-Controller pattern.

The Model-View-Controller pattern is not new. However the release of ASP.NET MVC (I’ll refer to it as "MVC" going forward) has renewed interest in this pattern.

MVC consists of three main parts. You’ll never guess what they are. Give up? They are the Model, the View and the Controller.

The Model contains the application’s data and any business rules associated with that data. It is up to the Model to retrieve and update data from and to a database (or other persistent data store or back-end), to validate that data and to apply any business rules to that data.

The View is a template for the user interface. It should contain no logic at all. In fact, in the ASP.NET MVC framework, by default View web pages don’t even have a code-behind file. A view in this framework consists only of HTML mixed with some placeholders to output values from the model or helper functions based on those values.

The Controller is the glue that binds the Model to the View. By default, each web request in ASP.NET MVC gets routed to a given method (known as an “Action”) of a given class (known as a "Controller"). This method executes and returns either a view or enough information for the framework to identify and render a view.

MVC has two main advantages over web forms –It is easier to test and it gives more control to the developer.

Separating an application into three distinct parts makes it far easier to test.  Launching a user interface in an automated testing framework, such as NUnit or MSTest is possible but it’s clumsy and slow. Minimizing the code in the user interface allows us to test the code more easily.

MVC also gives the developer more control over the rendered HTML (the view) than is possible in web forms. Web forms deliberately abstract away much of the HTML rendering, while MVC forces the developer to write explicit HTML in his View templates.

The release of ASP.NET MVC certainly does not signal the end of web forms.  Many developers appreciate the abstraction that web forms provides for them. Many web forms still exist in production and I have yet to hear anyone refer to these as "legacy" applications or "Classic ASP.NET".

But for those who want more control over their output, a more pure separation of concerns and easier testability, ASP.NET MVC offers a good option.

Thursday, 01 October 2009 13:39:21 (GMT Daylight Time, UTC+01:00)
# Wednesday, 30 September 2009

Episode 56

In this interview, Microsoft Architect Evangelist Brian Prince describes what makes Windows 7 faster, more reliable and simpler.

Wednesday, 30 September 2009 13:16:25 (GMT Daylight Time, UTC+01:00)
# Monday, 28 September 2009

Episode 55

In this interview, Microsoft IT Evangelist Matt Hester describes the new features and enhancements in the upcoming Windows Server 2008 R2.

Monday, 28 September 2009 05:09:10 (GMT Daylight Time, UTC+01:00)
# Saturday, 26 September 2009

We have been hosting Grok Talks at Sogeti since my arrival. Recently we decided to make them available via LiveMeeting and record the presentation. Here is a Grok Talk from September 23 2009.

In this presentation, Sogeti Principal Consultant Dr. David Truxall discusses the challenges of debugging and how to use WinDbg to debug production issues.

.Net | Grok Talk | Sogeti | Video
Saturday, 26 September 2009 16:09:26 (GMT Daylight Time, UTC+01:00)
# Friday, 25 September 2009

Episode 54

Kirstin Juhl came to software development from a career in manufacturing, where she learned about Lean principles. Now she sees those same principles being applied to software development. In this interview, she describes Lean in both worlds and compares the two.

Friday, 25 September 2009 12:24:22 (GMT Daylight Time, UTC+01:00)
# Wednesday, 23 September 2009

Episode 53

Joe Kunk is writing a chapter covering XTraReports for Paul Kimmel's upcoming DevExpress tools book. In this interview, Joe describes XTraReports and how to use it.

Wednesday, 23 September 2009 05:10:51 (GMT Daylight Time, UTC+01:00)