Use the Source

This article describes how to use the MVC 3 sources directly from a Visual Studio project.

Download the MVC Sources

The current MVC 3 sources are distributed in a zip files named mvc3-rtm-sources.zip available from Codeplex. I like to extract the zip file to a directory named MVC that is in the same parent directory as any Visual Studio project directory that will consume these sources so that the project references that my projects use will be relative references and not absolute references. So let’s say you keep your Visual Studio project(s) in the directory H:\projects. In that case, extract the MVC zip file to H:\projects\MVC. You will end up with the following directories in your H:\projects directory:

  • MVC

  • MyMvcProject1

  • MyMvcProject2

After extracting the MVC zip file, I immediately go to the MVC directory and rename the mvc3-rtm-sources directory to 3.0. At this point, it is a good idea to open The WebRuntime.sln file with Visual Studio and make sure you can build the solution. If you have any problems with the build, hit up the official ASP.NET MVC forum or the almighty Stackoverflow site.

The MVC 3 source tree looks like this:

Notice that there are two branches to the sources: the mvc3 and the webpages directories, which are in the same top-level source directory that contains the Visual Studio solution file. After building the solution, all of the mvc3 projects direct their build outputs to the mvc\bin\<config-name> directory and the webpages projects direct their build outputs to the webpages\bin\<config-name> directory.

In order to use the assemblies that are created from the MVC solution in a project that is has a strong name (i.e., generates a signed assembly) then you will need to modify the MVC solution slightly in order to have it generate signed assemblies. First, create a key to sign the assemblies with. Bring up a Visual Studio command prompt and set your directory to the MVC\3.0 directory, and generate a key file:

cd/d h:\projects\mvc\3.0

sn -k mvc.snk

sn -p mvc.snk public.snk

sn -tp public.snk

The last command will display output similar to this:

Public key is

002400000480000094000000060200000024000052534131000400000100010023558ed663ac3a

59a40cc61b291293d6ba2ea19e00ebe252121497af20ef5e6a4211bb73570c1fcee97b4b4b8e89

a667274e22561c1c7412a9a26ed13d6d74a8677262820ff9ff2ee50bca8d862b6d32390e82f2c6

9d07afc30f5a5bb1aca1321aa2f513a9cf18b5186a685472405389bab29eafacb1bb1a35c67fce

67dbb8a3

Public key token is 7e10774ed6578c6d

The first command creates a public/private key-pair file named mvc.snk in the current directory which can be used to sign the MVC assemblies. The second command extracts the public key from the key pair file to a separate file. And the third command displays the public key and the public key’s token from the public key file.

To modify the MVC solution so that it generates signed assemblies, open the WebRuntime.sln solution with Visual Studio. Right-click the Solution node and select AddExisting Item. In the Add Existing Item dialog, double-click the mvc.snk file. This will create a folder in the solution called Solution Items with the mvc.snk file listed in it.

Right-click the System.Web.Mvc project node and select AddExisting Item. In the Add Existing Item dialog, choose All Files (*.*) from the file type filter drop-down. Browse three directory levels up (to the h:\projects\mvc directory) and highlight the mvc.snk file—do not double-click the file—then select Add As Link from the Add drop-down. This will add the mvc.snk file to the System.Web.Mvc project as a link to the physical file in the Solution Items folder. Now right-click the System.Web.Mvc node again and select Properties. In the Signing tab, click the Sign the assembly check-box and then choose the mvc.snk file from the Choose a string name key file drop-down. This tells Visual Studio to sign the project’s generated assembly when the project is built.

Copy the public key that is displayed by the sn utility to a text editor and change it so that it is one big line (the public key is a string of 320 hex digits). Now open the project’s AssemblyInfo.cs file and change this line:

[assembly: InternalsVisibleTo("System.Web.Mvc.Test"]

to read:

[assembly: InternalsVisibleTo("System.Web.Mvc.Test, PublicKey=002400...dbb8a3")]

You must add the mvc.snk file to each project, and for the non-test projects edit the AssemblyInfo.cs file, for the following projects so that they will generate signed assemblies. Of the 26 projects (assemblies) in the MVC 3.0 solution, the following ones must be signed:

  1. System.Web.Mvc

  2. System.Web.Mvc.Test

  3. System.Web.Helpers

  4. System.Web.Helpers.Test

  5. System.Web.Razor

  6. System.Web.Razor.Test

  7. System.Web.WebPages

  8. System.Web.WebPages.Test

  9. System.Web.WebPages.Razor

  10. System.Web.WebPages.Razor.Test

  11. System.Web.WebPages.Deployment

  12. System.Web.WebPages.Deployment.Test

  13. System.Web.WebPages.TestUtils

  14. UnitTestUtil

In summary, each project listed must have the following steps performed on them:

    1. Add a link to the file mvc.snk.

    2. Change the project properties so that it is signed with this file.

    3. Edit each InternalsVisibleTo attribute in the projec’t AssemblyInfo.cs file to include a PublicKey value.

Now build the solution. You now have all of the MVC sources on your system so let’s use them from our own Visual Studio project.

Using the MVC Sources in Your Project

Before proceeding, make sure you have checked in all of your project’s code into whatever source control system you use and then maybe start a new branch and try these steps out from this new branch. If you aren’t using a source control system then make a backup copy of your project directory.

Now, open up your MVC-based Visual Studio project (or create a new one) and make sure the project builds without any errors. Right-click on the solution node at the top of the Solution Explorer window, and select AddNew Solution Folder. Name this folder External Projects.

Right-click on the External Projects node and select AddExisting Project. Browse to the MVC\3.0\mvc3\src\SystemWebMvc directory and open the System.Web.Mvc.csproj project file.

Make sure that your project still builds without errors.

Since you will be using the MVC sources instead of pre-built MVC assemblies you must go to the References folder of each project in your solution and remove any existing references to the System.Web.Mvc.dll assembly. Don’t forget to remove this reference from your MVC site’s test project if you chose to create one of those when you created your MCV project with the Visual Studio wizard.

For each of those projects that you removed the pre-built System.Web.Mvc.dll assembly from, you must now go back and add a reference to the SystemWebMvc source project. Right-click the References folder of one of the projects you removed the pre-built assembly from and select Add Reference. In the Add Reference dialog, go to the Projects tab, and add System.Web.Mvc to the project. Do this for every project from which you removed the pre-built assembly.

An Excursion Into the Telerik Extensions for ASP.NET MVC

If you use the Telerik Extensions for ASP.NET MVC controls in your MVC project you should also download the the source for these controls which are hosted on Codeplex. As of this writing (8/12/11) the version of these controls is 2011.Q2.712 and the zip file is named telerikaspnetmvc-2011.q2.712.zip. The zip file contains the sources for the Telerik.Web.Mvc.dll assembly as well as some test assemblies. The Visual Studio solution has build configurations that support MVC versions 2 and 3.

Once downloaded, extract the zip file to TelerikMVCExtensions\<version-number> (e.g., TelerikMVCExtensions\2011.q2.712).

Open the Telerik.Mvc.sln file with Visual Studio and then select the Debug MVC3 or Release MVC3 config that you want to build. Change the target framework for the Telerik.Web.Mvc project from .NET 3.5 to .NET 4.0 since this is the version that MVC uses.

Build the solution and make sure that no errors are generated. Note that the build generates a signed assembly.

Add the Telerik.Web.Mvc source code to the External Projects folder for your project.

Make sure your solution still builds.

Remove the references to the pre-built Telerik.Web.Mvc.dll assembly and replace them with a project reference to the Telerik.Web.Mvc source code project.