Stop Using [Tokens] and Just Use Razor

stop using tokens just just razor syntax

Stop Using [Tokens] and Just Use Razor

razorTokens are a standard way to inject dynamic content into a template without knowing anything about programming. A regular user can enter a token such as [USERNAME] into a template to get a personalized message to the user.

Tokens then started to get a little more advanced allowing you to add some attributes to modify the original data. Commonly you’ll see something to modify datetime stamps [DATECREATED:mm/dd/yyyy].

Lastly developers needed to implement a simple looping structure to allow a user to dynamically modify where and how the template is presented. Again this would look something like [ROWS] Additional Template [/ROWS]

There are a number of issues with this type of solution but the top three that come to mind are:

  1. It’s time consuming as a developer to create the replacement system for each and every token and future more how to handle all the mutations on their attributes
  2. It’s a proprietary system that each developer implements slightly different.
  3. Does not allow for additional tokens to be created in the future unless the developer takes the additional steps to build a mechanism or UI to handle.

Now we have Razor Syntax

Razor solves all of the issues above. It’s a standard system that already has functionality for conditions, loops, string interpolation, formatting, and allows the user to call any .net function to extend the functionality. But you may be thinking that this is all programmer hocus pocus how will a novice user use this?


Slightly more verbose than [USERNAME] but now adds 100 times the functionality. Say you want to lower case the username:


All that functionality is built into the engine

You can see a Quick Reference Guide created by Phil Haack that illustrates the a few other use cases.

Razor is not just for MVC. You can add the razor engine to any web forms app. Check out RazorEngine to parse a model and template together to produce an html output.

Using Razor in DNN

DNN Platform has the Razor engine built in and allows you to render a razor template in just a few lines of code. You will have access to details contained in the ModuleContext and the ability to use Localization. Additional helper classes will also allow you to gain access to properties about the Tab, Portal, Module, User, etc. These can be access using @Dnn.Tab or @Dnn.User. Check out Helpers for a full  list of properties that are available to you.

To use the DNN RazorEngine in your application

protected void Page_Load(object sender, EventArgs e)
	string templatePath = ControlPath + "Templates/Example.cshtml";
	var razorEngine = new DotNetNuke.Web.Razor.RazorEngine(templatePath, ModuleContext, LocalResourceFile);
	var writer = new StringWriter();
	placeHolderOutput.Controls.Add(new LiteralControl(Server.HtmlDecode(writer.ToString())));

Using a custom model in your razor scripts

The key to getting this to work is assigning the @inherits attribute to the top of your razor file and then using the razorEngine.Render(writer, new CustomEntity() {SomeProperty = "Value"}) overload.

@inherits DotNetNuke.Web.Razor.DotNetNukeWebPage<CustomEntity>


protected void Page_Load(object sender, EventArgs e)
	string templatePath = ControlPath + "Templates/Example.cshtml";
	var razorEngine = new DotNetNuke.Web.Razor.RazorEngine(templatePath, ModuleContext, LocalResourceFile);
	var writer = new StringWriter();
	razorEngine.Render(writer, new CustomEntity {SomeProperty = "Value"});
	placeHolderOutput.Controls.Add(new LiteralControl(Server.HtmlDecode(writer.ToString())));

Now you have a fully dynamic template that is capable of doing everything you need it to do with the full functionality for your user without limiting the type of modifications they can make. No matter how custom or advanced.

Jonathan Sheely

Sr Software Engineer at Maxiom Technology. Jonathan is an out of the box thinker who has over 10 years experience building and supporting web application software and infrastructure. Jon specializes in ASP.NET C#, Javascript and CSS but is willing to learn anything that gets the job done.

  • Jason Brunken
    Posted at 3:28 pm, March 17, 2014

    Great article!

    Can razor be used with sub-templates? Case in point: If my model contains a property that is another object, can I render that object using it’s own template?

  • Posted at 3:30 pm, February 6, 2015

    Thanks Jonathan – great post!

    I did have an issue in getting the Localization to work as I was calling:
    And every overload for it passing in the LocalResourceFile – but nothing ever showed.

    I finally realized to call:
    And I was off to the races again.

    Just wanted to throw that out if anyone else comes across this post and runs into that issue.

  • Posted at 7:51 am, March 31, 2015

    Funny as I was running in this loop again and just scrolled to see my own comment answered my issue again. (I’ll make sure this makes it in my code repo this time 😉

    One fix: It should be:

    As in past tense with the d on localized

  • Stephen Bugden
    Posted at 3:51 pm, May 19, 2015


    I can’t get the example to work. Can anyone tell me what to write in the Example.cshtml file to write out the property value?

    Best Regards,