Lansing Day of .NET 2009

Last Saturday (August 1st) was Lansing's Day of .NET.  The guys organizing this did a great job and I had a really fun time.  I gave my "Intro to ASP.NET MVC" presentation.  I got some great questions during the presentations as well as good feedback.  I did have a few questions that I wanted to follow up on:

Using WebForms Controls in ASP.NET MVC

I mentioned that it should be possible to re-use ASP.NET Webforms controls if they don't rely on ViewState.  I've never done it before, but knowing how web forms controls do their rendering, I thought it would be quite possible to re-use WebForms controls as simply generators of HTML.  I did a few Google searches and found out it's incredibly easy.

All you need to do is register the assembly that contains the user control.  In this example, I registered the System.Web assembly and the System.Web.UI.WebControls namespace so I could access the WebForms <asp:Table> control.  I used the tag prefix of "wc" to indicate a WebForms control:

<%@ Register Assembly="System.Web" Namespace="System.Web.UI.WebControls" TagPrefix="wc" %>

Now using the controls in the System.Web.UI.WebControls namespace is as easy as it was in WebForms.  Here's a sample for one of my MVC views:

    <wc:Table runat="server" id="wfpan">
        <wc:TableRow>
            <wc:TableHeaderCell>Hdg1</wc:TableHeaderCell>
            <wc:TableHeaderCell>Hdg2</wc:TableHeaderCell>
        </wc:TableRow>
        <wc:TableRow>
            <wc:TableCell>Cell Data</wc:TableCell>
            <wc:TableCell>More Cell Data</wc:TableCell>
        </wc:TableRow>
    </wc:Table>

Again, if the control relies on ViewState for any behaviors, this probably won't work too well.  But you can at least utilize it as a simple HTML generator.

Eliminating "magic strings"

Many people who first see ASP.NET MVC code notice redirect code like this:

return RedirectToAction("Index");

This redirects to the "Index" method of the current controller.   The problem is that if you rename your Index method, this string will not get updated (well, this assumes you're not using ReSharper which would fix the string too – but that's a different story).  If you hate magic strings as much as I do, check out the MvcContrib project on CodePlex.  They've got some extension methods that let you use lamdas to express your redirects:

return this.RedirectToAction(c => c.Index());

If you need to redirect to a different controller, you can specify a controller type in the call:

return this.RedirectToAction<AccountController>(c => c.LogOn());

Very cool!  No more magic strings!  For more information, see the MvcContrib project and this blog post.

Sample Project Code

Finally, I've had a few people asking for the sample code I wrote during the presentation.  Since I didn't get into as much detail as I wanted during the presentation, I'll ZIP up a reference project I used to create the presentation and make that available for download.  It should be up by end of day today.

Technorati Tags: ,,

No Comments