January 2010 Entries

KISS Your ASP.NET MVC Routes

A little bit of thinking and compromise can remove unnecessary complexity from the routes in an MVC application. For example: Morgan’s web site has authenticated users,and Morgan decides the URLs for managing users should look like /morgan/detail. Morgan adds the following code to register the route: routes.MapRoute( "User", "{username}/{action}", new { controller ="User", action="Index" } ); Morgan runs some tests and it looks like there is a problem. The User controller is getting requests for /home/index. Oops, that request should have gone to the Home controller. Fortunately, Morgan knows...

Silverlight and ASP.NET MVC Don’t Serve the Same Master

I’m flipping between MVC and Silverlight projects when a startling contrast starts to emerge. It’s not the obvious contrast between stateful and stateless. It’s a subtler contrast in design. If you poke around in a new MVC project, one of the first pieces of code you’ll run across is the code to register the default route. It’s in global.asax.cs, and looks like this: routes.MapRoute( "Default", ...

Drop-down Lists and ASP.NET MVC

Working with drop-down lists in ASP.NET MVC has some confusing aspects, so let’s look at an example. Imagine the goal is to edit a song (not the music and lyrics of a song – just the boring data pieces). Each song is associated with an album, and each song has a title and track number. With this description, you can imagine an edit view using the following code: <%= Html.DropDownList("AlbumId", Model.Albums)%> ... <%= Html.TextBox("Title", Model.Title) %> ... <%= Html.TextBox("TrackNumber", Model.TrackNumber) %> The Html.DropDownList helper method likes to work with SelectListItem objects, so a view model you can...

T4MVC in MSDN Magazine

The January 2010 issue of MSDN Magazine is online with my article covering T4MVC: Microsoft Visual Studio includes a code generation engine known as T4 (which is short for Text Template Transformation Toolkit). You’ve probably already used T4 templates in Visual Studio without even knowing they were working behind the scenes. In this article I’m going to give you a basic introduction to T4 templates and show you how ASP.NET MVC uses this technology. I’ll also show you how to customize T4 templates to enhance your day-to-day work with the MVC framework Thanks...

Of Web Browsers and Humanity

Douglas Crockford posted an interesting topic for discussion on his site (look for the Discussion Topic section at the bottom of the page): If a web browser is defective, causing errors in the display or performance of the page, should the page developer struggle to hide the browser's defects, or should the defects be revealed in hope of creating market pressure to force the browser maker to make good? By which approach is humanity better served? What I’d Like To Say When I was a kid, I loved Mad Libs. And I bet...

Don’t Let Your Code Marry An Axe Murderer

It’s a lesson I learned from the school of hard knocks: be careful about the shady characters you let into your software. There are lots of software frameworks, components, and tools in the world – and they all want you to think you can live with them happily ever after. They seduce you with the promise of productivity, then hack you to death once they’ve gained your trust. Here are a few questions I’ve learned to ask when trying to spot next year’s problems: 1. How easy is it to deploy? I’ve been...