Execute Razor
Audience:
Low-code EngineersSkill Prerequisites:
C#,Razor,Tokens
Runs a Razor script on the server. Razor mixes C# code with text or HTML. Whatever the script writes out is stored in a token.
All tokens in the context are available in the script as variables, for example @Email. The script is compiled the first time it runs and reused after that.
Typical Use Cases
- Build an HTML snippet, like a table or an email body, from tokens and C# logic
- Format dates, numbers or text with .NET methods
- Use .NET or DNN classes that aren't available through tokens or other actions
- Use a class from a custom assembly in the
binfolder
Don't use it to
- Run JavaScript. Use Execute Javascript (Server).
- Build a simple message from tokens. Put the tokens directly in the text of the action that uses it.
- Run SQL. Use Run SQL Query.
Related Actions
| Action Name | Description |
|---|---|
| Execute Javascript (Server) | Runs JavaScript on the server. |
| Run SQL Query | Runs SQL and stores the results. |
Input Parameter Reference
| Parameter | Description | Supports Tokens | Default | Required |
|---|---|---|---|---|
| Script Name | A unique name for the script, for example OrderSummary. It's used to cache the compiled script. | No | empty string | Yes |
| Local Variables | Extra variables for the script. Each row has a Type (string, int, double or bool), a Variable Name and a Variable Value. The name must be a valid C# identifier, like taxRate. The value can contain tokens and is converted to the chosen type. | Yes (Value only) | empty | No |
| Additional assemblies | Extra .NET assemblies the script needs, one per row. For an assembly in the bin folder, use ~/bin/Your.Assembly.dll. Paths starting with ~ or / are relative to the website root. | Yes | empty | No |
| Code | The Razor code to run. Use @{ } for C# code and @ to write out values. Tokens in square brackets aren't replaced here. Use the token's variable instead, for example @Email rather than [Email]. | No | empty string | Yes |
| Store Result | The token name that holds everything the script writes out, for example Summary. | No | empty string | No |
Output Parameters Reference
| Parameter | Description |
|---|---|
| Store Result | Holds the text the script writes out, with leading and trailing whitespace removed. If the script fails, it holds an error message instead. See Errors. |
Available Variables
| Name | Description |
|---|---|
| Context tokens | Every token in the context, such as form fields, workflow inputs and tokens created by earlier actions. A token named Email is @Email. Their type is dynamic. |
| Grouped tokens | Tokens with a colon in their name are grouped into one object. For example, [Guest:Username] becomes @Guest.Username. |
| Token namespaces | Custom tokens, accessed as Namespace.TokenName, for example @Company.SupportEmail. See Custom Tokens and Namespaces. |
Tokens | All of the above, as one dynamic object, for example @Tokens.Email. |
| Local Variables | The variables defined in the Local Variables parameter. They override context tokens with the same name. |
User | The current DNN user (UserInfo). |
Portal | The current portal settings (PortalSettings). |
Tab | The current page (TabInfo). It's empty when nothing started the actions from a page, for example in a scheduled job. |
Module | The module that started the actions (ModuleInfo). |
Request, Response, Server, Session, Application | The ASP.NET objects for the current request. They're empty when there's no web request, for example in a scheduled job. |
Namespaces and assemblies
These namespaces are imported, so you don't need @using for them: System, System.Linq, System.Collections, System.Collections.Generic, System.Web.WebPages, DotNetNuke.Entities.Users, DotNetNuke.Entities.Portals, DotNetNuke.Entities.Modules and DotNetNuke.Entities.Tabs.
The script can use System.dll, System.Core.dll, System.Web.dll, System.Xml.dll, Microsoft.CSharp.dll, DotNetNuke.dll and DotNetNuke.Abstractions.dll without adding them. Add any other assembly in Additional assemblies.
Errors
A compile or runtime error doesn't stop the action. The error is logged, and Store Result gets a message instead of the output:
| User | Store Result contains |
|---|---|
| Administrators and users who can edit the module | Error compiling Razor Template: <message> (check logs for more details) |
| Everyone else | Error compiling Razor Template (contact the administrator for more details) |
If later actions depend on the result, check it with a condition first.
Considerations
- Licensing. This action needs the
Scriptingfeature in your license. - Output isn't HTML-encoded. Values written with
@are output as they are. Encode user input yourself, for example with@System.Web.HttpUtility.HtmlEncode(Comment), before you show it on a page. - Compiled once. The compiled script is reused until the code or Script Name changes. Each change compiles a new version, which stays in memory until the application restarts.
- Token names must be valid C# names. Each token becomes a property of the script, so an unusual token name can cause a compile error.
- Tokens aren't replaced in Code. Use variables, not
[Token]syntax. - Local Variables are checked first. An invalid type, name or value stops the action with an error.
- ASP.NET Web Pages. The server needs ASP.NET Web Pages with Razor Syntax. If it's missing, the error message says so.
- The script can't change tokens directly. Only the output comes back, through Store Result.
Examples
To understand how to use the below examples, please see Running Examples.
1. Calculate a due date
This action adds the number of days in the Days local variable to today's date. The result, for example 2026-10-26, is stored in [DueDate].
{
"Title": "Execute Razor",
"ActionType": "ExecuteRazor",
"Description": "Calculate the invoice due date",
"Parameters": {
"ScriptName": "InvoiceDueDate",
"LocalVariables": [
{
"Type": "int",
"Name": "Days",
"Value": "30"
}
],
"Assemblies": [],
"Code": "@DateTime.Today.AddDays(Days).ToString(\"yyyy-MM-dd\")",
"OutputTokenName": "DueDate"
}
}
2. Build an HTML greeting
This action writes a short HTML block with the current user's name and the OrderId token. The result is stored in [Greeting], ready to use in an email.
{
"Title": "Execute Razor",
"ActionType": "ExecuteRazor",
"Description": "Build the order confirmation greeting",
"Parameters": {
"ScriptName": "OrderGreeting",
"LocalVariables": [],
"Assemblies": [],
"Code": "@{\n var hour = DateTime.Now.Hour;\n var greeting = hour < 12 ? \"Good morning\" : \"Hello\";\n}\n<p>@greeting @User.DisplayName,</p>\n<p>Thank you for order <strong>#@OrderId</strong>.</p>",
"OutputTokenName": "Greeting"
}
}
3. Read a JSON value with Newtonsoft.Json
This action adds Newtonsoft.Json.dll from the bin folder, parses the OrderJSON token and writes out its total value to [OrderTotal].
{
"Title": "Execute Razor",
"ActionType": "ExecuteRazor",
"Description": "Get the total from the order JSON",
"Parameters": {
"ScriptName": "OrderTotalFromJson",
"LocalVariables": [],
"Assemblies": [
"~/bin/Newtonsoft.Json.dll"
],
"Code": "@{\n var order = Newtonsoft.Json.Linq.JObject.Parse(Convert.ToString(OrderJSON));\n}\n@order[\"total\"]",
"OutputTokenName": "OrderTotal"
}
}
Revised 09/26/2026