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:
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:
The last command will display output similar to this:
Public key is
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 Add → Existing 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 Add → Existing 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:
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:
In summary, each project listed must have the following steps performed on them:
Add a link to the file mvc.snk.
Change the project properties so that it is signed with this file.
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 Add → New Solution Folder. Name this folder External Projects.
Right-click on the External Projects node and select Add → Existing 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.