Creating Razor Functions

Create CMS Functions using Razor

Creating the Razor functions differs from creating any of 4 types of CMS Functions.

Creating Razor functions

To create a Razor function:

  1. Under ~/App_Data/Razor, create a cshtml file. (In Visual Studio, select the "Empty Page (Razor)"  template.)
  2. Use folders to reflect the namespace of your function and the meaningful file name that will stand for your Razor function's name: e.g. ~/App_Data/Razor/My/Razor/Function.cshtml will end up in the C1 CMS's GUI as the "My.Razor.Function".
  3. Make sure it inherits from CompositeC1WebPage: @inherits CompositeC1Contrib.RazorFunctions.CompositeC1WebPage
  4. Add needed references with @using. For example, if you are going to use C1 CMS's data system add: @using Composite.Data  and @using CompositeC1Contrib.RazorFunctions
  5. Add/change your code and markup as you need.
  6. Save the file.

C1 CMS is aware of a newly added cshtml file and the new function will appear in the Select Function dialog instantly.

Since the Razor Functions are created as cshtml files under ~/App_Data/Razor (one cshtml file for one function) and not in a Function Designer in the GUI, you can define parameters by declaring them in their cshtml files as public 'get' properties. By default, the function's return value is of the XhtmlDocument type. You can change the return type to any supported in C1 CMS.

All the cshtml files that serve as Razor functions should inherit from a common CompositeC1Contrib.RazorFunctions.CompositeC1WebPage, which in turn inherits from System.Web.WebPages.WebPage.

Hence, you have access to the normal helper methods and properties such as Html, Context, Layout, Page or PageData. (Since it is not the same as a MvcWebPage it can't use Model or Controllers, though.) You also have access all the usual .NET functionality.

As they inherit from CompositeC1WebPage, you have access to Composite.Data and CurrentPageNode, too.

Further improvements and closer integration with C1 CMS is expected.

Using Razor functions

You can use Razor functions as other types of CMS Functions. You can insert them in the Visual Editor or Source Editor, use them as a function call in XSLT functions or as a value for other functions' parameters.

(Read more on using CMS Functions.)