Razor Mediator: Including Code From An External DLL

The Razor Mediator allows you to easily add other libraries and namespaces to your Razor templates.  Due to an issue found with version one, you will want to get the latest installer from the Google Code site (or just download the latest version 1.0.1 installer here).  If you have version 1.0.1 or later you should be in the clear and hopefully problem free.

For this tutorial we’re just going to create a really simple class in a separate project and then allow our Razor templates to use that new class. After this tutorial you should have a pretty good understanding of how to use the namespaces and assemblies sections of the razor.mediator configuration.

First, create a new project in Visual Studio.  We’ll name it “RazorSample.Test”.

Next, create the following class in your new project.

namespace RazorSample.Test
    public class MyClass
        public static string Hello()
            return "Hello World!";

        public static string Hello(string name)
            return "Hello " + name + "!";

I did mention that it would be a simple class! We’ve got two methods and I think it can be safe to assume that I don’t need to explain what they do. Compile the project, and copy the newly created DLL to your CMS server. For this tutorial, I’ve put mine at C:MyLibraryRazorSample.Test.dll.

Next you’ll want to modify the Tridion.ContentManager.config file located at %Tridion Install Path%config on your Tridion CMS server. Search for the razor.mediator section, then update the assemblies section to add your newly created DLL.

      <add assembly="C:MyLibraryRazorSample.Test.dll" />

Go ahead and restart the Tridion COM+ package.

Now go ahead and create a new Template Building Block in Tridion called “Razor Test” (or whatever you wish to name it). From the Source tab, make sure to select “RazorTemplate” for the Template Type, and then add the following:


Save and close. Now go ahead and open up Template Builder and create a new Compound Component Template. Add the “Razor Test” building block from the previous step, and then Run the template using any Component. You should see both of your newly created method returns in the output:

<div>Hello World!</div>
<div>A Test Component!</div>

Congratulations, you’ve just executed code from another assembly in your Razor Template! That’s great and all, but what if you want to load an assembly that’s in the GAC instead of on the file system? In order to get our sample assembly into the GAC, we’ll have to give it a strong name key. If you are unfamiliar with this, just proceed with the following steps.

  1. Right click the RazorSample.Test project in the Solution Explorer of Visual Studio and click Properties.
  2. In the left column, select the Signing tab.
  3. Check the “Sign the assembly” check box.
  4. In the dropdown underneath the “Choose a strong name key file:” label, select “”.
  5. In the Create Strong Name Key dialog, enter “RazorSample.Test” for the Key file name and uncheck the “Protect my key file with a password” option.
  6. Click OK.
  7. Save the project and recompile.

Once you have your RazorSample.Test project recompiled with a Strong Name Key, deploy it to the GAC on the Tridion server. For simplicity, I made the RazorSample.Test project target .NET 3.5 so that I could just drag and drop the DLL to C:Windowsassembly. Once your DLL is deployed to the GAC, edit the assemblies section in the Tridion.ContentManager.config file to look like the following.

    <add assembly="RazorSample.Test, Version=, Culture=neutral, PublicKeyToken=60ad7434f03dfcdc" />

You’ll notice that the only difference in the config between loading it from the GAC and from the file system is that the file system is an absolute file path while the GAC is the fully qualified name.  Remember, your Public Key Token will be different than the one you see above.  If you are unsure how to get the Public Key Token, find your DLL at C:Windowsassembly (assuming that DLL is .NET 3.5 or less).  You’ll see the info you need in the Public Key Token column, or by right clicking your DLL and selecting Properties.  Once the configuration is saved, restart the Tridion COM+ package again. Running the template in Template Builder again should result in the same output.

Now what about the namespaces section? Well unless you don’t mind using the full namespace to your classes, this is where that section will come in hand. Reopen the Tridion.ContentManager.config file once again and modify to look like the following.

  <add namespace="RazorSample.Test" />
  <add assembly="RazorSample.Test, Version=, Culture=neutral, PublicKeyToken=60ad7434f03dfcdc" />

After restarting the Tridion COM+ package, reopen your “Razor Test” Razor Template and modify the razor to look like:


Save and close. Restart the Tridion COM+ package. Retest your template. And enjoy the bliss of adding your own useful functionality to your Razor templates.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>