Current Development Must-Haves for ASP.NET MVC 3

I recently wanted to replace the exhaust manifold on my vehicle. I looked at the part online, and there were maybe 8 bolts that I needed to remove/replace.  As it turns out, those 8 bolts are part of a 112 step process that takes an experienced mechanic 5-6 hours to complete. If he has the right tools.

The ASP.NET 3 Framework gives us a lot of help in achieving our web application development chores, but it’s best to have the right tools.

Here’s a current checklist (July 2011) of the tools you should have in your kit:

  • A copy of Visual Studio 2010 (trial, express, free)
  • *   <div align="left">This one's important!&nbsp; VS 2010 SP1 gives you the most updated tooling for MVC.</div> <li>The NuGet Package Manager ([plugin](, [console](
    *   Also important! NuGet lets you access great libraries for your project, including MvcScaffolding, Ie9ify and more  <li>It's installed in SP1, but make sure you get the latest updates <li>The Visual Studio 2010 Productivity Power Tools ([plugin](  <li>An LED [Binary Clock]( It won't help you with MVC development, but it looks cool on your desk  <li>The Entity Framework [version 4.1](;id=26825)
    *   The "project version" binary is pulled down by new projects in MVC 3 through NuGet, but the full version adds other tooling and templates 


Also, check out this post I put together with a ton of handy links for your MVC 3 adventures.

Missing anything? What do you use on a day-to-day basis that’s not on this short-but-powerful list?

By the way, the only thing in my mechanic toolkit is a hammer and a set of pliers.  I left the exhaust manifold for the pros.


Questions about the ASP.NET MVC 3 Framework

Here is a collection of the most common questions I’ve run into when working with developers who are new to application development using the ASP.NET MVC 3 Framework.  I will continue to expand this list as questions come up, feel free to ask yours in the comments below!


Creating New Projects

What if I don’t like the look of the default project?

You really have two options here; you can edit the _Layout.cshtml file located in Views\Shared\ to make it your own (the Site.css stylesheet is in the Content folder), or you can opt to start with an empty project. You’ll get a very clean _Layout.cshtml at this point, and a really reduced Site.css to work with. Remember that getting your project going is as easy as adding controllers and views, but you won’t have the authentication bits that the Internet Application template gives you.

Is it okay to use the ASPX View Engine instead of Razor?

Of course! If you’re worried about learning the pattern as well as a new view engine, you can start with a syntax you’re more comfortable with. You will have a harder time finding source samples, however, as most folks on the MVC Framework are using Razor. Also note that there is a different paradigm at play in MVC. ASP.NET Web Forms use a PageController pattern and make use of view state. You don’t have those or page events in the same way in MVC, so don’t be surprised if you don’t find them!

Why should(n’t) I use HTML 5 semantic markup?

HTML5 and CSS 3 aren’t fully supported on all browsers and the implementations that are there aren’t yet consistent. What’s important to remember is that HTML5 is HTML4 plus an assortment of new features. If you’re targeting HTML5 and using the new features you’ll still need to write alternate views (degrade gracefully) unless you have a fixed deployment environment (like corporate intranet), however, it’s important to remember that the semantic tags do add value to your page today, they are backwards compatible (especially with Modernizr) and are now starting to be better read and understood by search engines.


Controllers and Views

Do controller actions have to return Views?

Not at all! You can return plain text, images, PDFs, page fragments (typically called partial views), XML or JSON…pretty much whatever you like.

Navigation isn’t something that can be fully free, but it can be easy. We could create a link in our _Layout.cshtml file, and this would be a great exercise for the reader. If you’re working from a clean project, you want to add a third LI element to the NAV’s UL. In the new LI, you can follow the pattern of the other menu items and use the HtmlHelper extension method to render links. Using Visual Studio’s IntelliSense you can figure out which order to pass in the label of the link, the action and the controller you’re looking for. Or just re-read that sentence.

When I type return View() how does it know which view to return?

This, again, is part of the beauty of convention over configuration.  The view that will be returned is the one that has the same name as the method in the controller. If you have a method in the Food controller with the following implementation:


…then you’re going to need a view named SpicyPickles in the Food folder inside of Views.

You’re not bound by this, however, and you can return alternate views if you like by passing the name of the view as a string:




Why are my model classes not showing up in the MVC tooling?

The most likely reason is that you have not built you application. The MVC tooling looks at types in your assemblies. If you haven’t built your app since you added those model types to your project (or modified them), the tooling won’t pick up all the correct bits for you.

I’m having trouble binding my model when submitting data to my controller. How do I debug the request?

There’s a couple of things you should try to do. First, get a copy of Firebug running (in Firefox) or use the developer tools in IE9 or Chrome.  You need to examine the message you’re sending to the server. Check to make sure that your parameters are named correctly, conform to the correct type and are actually capturing the correct values from the client. Set a breakpoint in your controller and examine the object. Make sure the correct action is being invoked.  Glimpse is also coming a long way in revealing how your request is seen by the server and can easily be installed via NuGet.



Do I have to use the ASPNETDB.MDB that is created for me for authentication?

No, you can setup your own database following these steps.

Setting up an Alternate Data Source for your ASP.NET Authentication Provider

imageThe authentication provider that is baked in to ASP.NET for free is a great start to managing users on your site, and gives you full capabilities around roles, profiles and authorization within your application.  The default project template for internet applications includes functionality designed around it. I’m a big fan of these free jump-starts as they’re also easy to swap out.  For the purposes of this post, I’m using Visual Studio 2010, the ASP.NET MVC Framework and SQL Server 2008 Express.

By default, if you try to create an account without prior configuration, the ASP.NET bits happily go about creating a local MDF for you with all the required tables and procs.  This is great, if you never intend to save anything else in your database, you don’t wish to rename it, and you don’t want to use an existing data source, but it’s also not the only way to go about getting a data source for your authentication needs.

If you’re scratching your head after creating a user account, wondering where the MDF ended up, look no further than Visual Studio’s magic show-me-my-flipping-files button.  The data file will be in your App_Data directory, but not included by default in the project.



Provisioning Your Own Data Source

Thankfully, the good folks at Microsoft have provided us with a tool to create these tables and procs in our own databases as well.

First, we’re going to need to get a landing spot for our data, so fire up SQL Server Management Studio and connect to your favourite database server. This can be any SQL Server edition (including Express, which is free), running locally or out on your network.  Create a new database, I called mine NotJustAnAuthDatabase.

Open a Visual Studio Command Prompt as Administrator and type the following:


While you can pass in arguments from the command line, there’s also a wizard that launches in the absence of them.  This makes it easy to select the database and apply the authentication data model. 

Step through the wizard.  We’re going to Configure SQL Server for application services.  Notice the option to remove the service information?  That’s why I said it’s easy to swap!


Next, we specify the server and database name that we want to add authentication support to:


Click next a couple times, then finish.  Your data source is all setup!  You can check your handiwork in SSMS:



Getting ASP.NET In Tune With Our Work

There are only a couple of small steps left to point our provider at the new database.  First, we’ll need to adjust our ApplicationServices connection string.  Pop back into Visual Studio and open up web.config.  You’ll need to work out whatever your connection string will be for your server and database. Mine looked like this:


Next, you can delete the MDF file that was created for you (if you had one created).  Remember to use the Show All Files button to reveal the database if it does not at first appear in Solution Explorer.  Oh, and a note here: you’re deleting a database.  It may hold data, so think that through if you want something in it.

When you re-run your app you will now be attached to the freshly-configured data source for authentication, allowing registration to your new backend.

A Few Cleanup Points

First, authentication isn’t the only thing that’s covered in Application Services, there is a lot of goodness baked in there. You might consider using profiles as well.  Once a user is authenticated, you can make use of the very simple authorization attributes in ASP.NET MVC, so there’s all kinds of peripheral benefits too, not just the benefit of not writing registration/role/authentication code for users on your site.

Secondly, this isn’t new.  In fact, the first bits of these services were shared with us in late 2005 when we were introduced to the .NET 2.0 Framework.  So why am I posting about it now?  As easy as it is to use, as comprehensive as the toolkit seems, and as mature as the components are, I still run into senior developers who have never used it or explored it.  File –> New, people! File –> New!!!

Another reason is that there are lots of new developers out there who aren’t sure by looking at VS2005 docs if that material is relevant any more. Some of it isn’t. Some is, but the IDE looks different.  It may seem cumbersome at first glance to get all this going, but I’m hoping that by showing you all you have to do is run a wizard and update a string that you’ll consider giving it a try.

Finally, just remember that if you’re mid-stream on a project and you make a switch out you’re going to need to first back up your data. Setting up the service components in an alternate location doesn’t port your users or roles and if you nuke your existing data store that data will be gone.

Best of luck in your authentication adventures!

Back on the Bike

Work has been overly busy lately, almost draining, as I’m in high season for maintenance.  With very little new work, it’s long been my challenge to keep motivated when switching from “hunter” to “farmer” mode.

Personally, we’ve had a rough streak as a family, too, having to endure now 12 funerals in the last nine months. This is one of the realities you have to face when you and your spouse have a close family, counting a combined (whopping) total of over 100 blood relatives, nearly all of which are older than you.  It’s been a sad couple of seasons at home.

Sweat to the Rescue

I have always found that getting my heart going helps to clear my mind.  I grew up being fairly active playing varsity high school sports and spent six years as a competitive swimmer.  I have cycled from Montreal to Banff on a national relay team benefitting Juvenile Diabetes and have put in over 7,000kms on the bike in the last three years.  But none in this calendar year.

I have always found that getting a bit of time to work the muscles and think through problems can help me make great strides.  In fact, I would argue that some of my best coding – or at least, solving some of my hardest programming challenges – has been away from the keyboard.

100kms and Counting

I’m no longer an ideal candidate for the swim team, nor did I fully grow to a size that any self-respecting volleyball team would give me anything other than bench time, but I do still have my road bike.  Technically, I still have my speedos too, but I’m not getting into those anytime soon! Winking smile

So my wife and I have reciprocally committed to exercise time and appropriate support for each other.  After supper, she heads out for a good brisk walk while the kids and I clean up supper.  When the kids head off to bed and she’s putting the baby down I’ve been jumping on my bike. 

In the last 4 days I’ve been increasing my kms and decreasing my time.  Last night I posted just over 26kms in a cool 54 minutes riding through the city, an average speed of about 29km/hr (18mph).

Today at work I started with more energy, cleaned off some outstanding maintenance issues and was rewarded by getting to spike out a new project out in WCF and WP7.

It’s amazing what a little exercise can do for you.

Adaptive Performance is Evil

I was working on tribute video for good friends of mine this last week using Adobe Premiere Pro CS4.  I have a brand-spanky-new laptop with some pretty rockin’ specs including an nVidia video card that chews through the video processing, for the most part, in real time and as fast as 5 times better.

But I couldn’t get that far into editing. Every minute to 90 seconds Premiere was crashing.

I was getting OpenGL errors, saying “A problem occurred when processing OpenGL commands”, or “Encoding Failed. Could not read from the source” or “The NVIDIA OpenGL driver detected a problem with the display driver and is unable to continue. The application must close”.  There was also one about “lost the connection to the device” and the error numbers from Adobe Premiere were error 4 and error 8.

The culprit? Adaptive Performance. It’s built into the GPU drive and by default turned on. It’s supposed to use less power and save the environment or something.

Turns out I hate planet Earth.

TURN ADAPTIVE PERFORMANCE OFF. At least for Premiere Pro.  If you go into the nVidia control panel you can actually adjust driver settings on a per-application basis (or globally set the feature’s status across the whole OS). Adaptive Performance is a feature that steps down the processing capabilities of your video card to conserve energy and then tries to crank the juice up when you need it.

Unfortunately this causes Adobe Premiere Pro to wig out.

So, turn it off and live happier.

A quick note: I found the solution to this on a gamer site when I was Binging the errors and I’m certain it was related to an OpenGL error. I would like to give attribution here but I don’t remember the name of the gaming community and can’t re-Bing the same results I was getting when I found the fix.

It's been a long, long Long Weekend

Thursday afternoon, as our family was preparing for our July long weekend, we received some terrible, terrible news.  Some of our closest friends’ daughter was killed in an accident on the highway; she was 17 years old.

She was driving up to her parents’ cabin at the lake to visit her mom and was killed instantly in a head-on collision.  Her boyfriend, travelling with her, also passed away.

Haven’t made much sense of things yet.  Because of the type of accident the autopsies had to be done in Winnipeg, about 2.5 hours from here.  As a result, the families won’t have their children back until late today or possibly tomorrow.

As a double whammy, we found out early Saturday that the driver of the other vehicle is another person close to us, an adopted aunt, of sorts, that has been in my wife’s family for years and years. We’ve eaten together every Christmas, Easter and Thanksgiving for the last 17 years (when I started dating my wife).  She’s now in the trauma unit with broken…everything…on life support and requiring many surgeries before she makes it out.  It is nothing short of miraculous that she’s come through this and she has such a long road ahead of her.

My birthday was on July 1, Canada Day.  My wife and kids and I took in the fireworks and had some good family time together, which helped to take our minds off the senselessness of it all.

Funerals are planned for Friday and Saturday for the kids.  We’ll be back and forth to Winnipeg to visit our friend as soon as they allow visitors.  It’s going to be a long week.