Code Effects ASP.NET WebForms 4.5 Implementation Example

The Code Effects engine is formerly known as Web Rule

Below are the basic steps to integrate Code Effects into a classic ASP.NET WebForms application. Note that this article provides code excerpts taken from the ASP.NET WebForms demo project.


  • Your project must be compiled with at least .NET Framework 4.6.2. To verify, right-click the project node in Solution Explorer, select Properties, select Application tab, and make sure that the Target Framework lists ".NET Framework 4.6.2" or a higher version.
  • Add Rule Editor to your web project by referencing its assemblies from NuGet. Please note that Rule Editor requires different assemblies on different .NET web platforms. The correct NuGet package for Rule Editor on ASP.NET WebForms 4.5 and below is listed here.
  • Optionally, reference either your licensed assembly of Rule Engine, or the trial assembly from NuGet if you plan to evaluate rules in your web application. Remember that the Engine Trial license delays all calls to Evaluate() and Filter() methods for six seconds.
  • In the classic ASP.NET WebForms you don't need to reference Rule Editor's main script or CSS file(s) on your pages, everything that Rule Editor needs is compiled into its assemblies.


First, let's create a .NET class called Person with three public properties that will be used as rule fields, and two public void methods that we can use as rule actions. We are going to use this class as a source object for our rules. In short, the source object represents the data that must be evaluated against the rules that users create using Rule Editor. Although you can decorate your source object and its members with attributes provided by the CodeEffects.Rule.Attributes namespace to control their use, validation, evaluation, or rendering, in this example we will just use a plain class in order to keep things simple:

namespace TestLibrary
	public class Person
		public string FirstName { get; set; }
		public string MiddleName { get; set; }
		public string LastName { get; set; }

		public void ActionOne() { /* some action logic goes here */ }

		public void ActionTwo() { /* some other action logic goes here */ }

Now we need to register Rule Editor on a web page, and assign the Person class as its source object:

<!-- Only Code Effects-related markup is shown -->

<%@ Register assembly="CodeEffects.Rule.Editor.Asp"
	namespace="CodeEffects.Rule.Asp" tagprefix="rule" %>

<rule:RuleEditor ID="ruleEditor" runat="server"
	SourceType="TestLibrary.Person" />

<asp:Button ID="btnGo" runat="server" Text="Go" OnClick="Go" />

Setting the Mode property to Execution allows us to use Rule Editor to create both types of rules - execution and evaluation. The default value of the Mode is Evaluate which allows creation of only evaluation type rules. Setting ShowToolBar to False hides the Toolbar, so we must provide our own mechanism to save and load our rules. The Go button takes care of that.

The above code is all we need to be able to run the page and produce our first business rule on the screen:

	FirstName is equal to Paul
	or LastName starts with D

and MiddleName contains James

	then ActionOne

else ActionTwo

The next step is to store the rule that we just created in a database in order to make it available later. We can do this in our code-behind when the Go button is clicked:

using System;

namespace TestWebApplication
	public partial class WebForm1 : System.Web.UI.Page
		protected void Go(object sender, EventArgs e)
			// Save the rule to database
			if (!ruleEditor.IsEmpty && ruleEditor.IsValid)
				string ruleXml = ruleEditor.GetRuleXml();

In real life, the instances of Person class would probably come from some external source, and would be evaluated against our rule by some .NET process somewhere on our corporate network. But here we will just create a small program that manually instantiates two instances of Person, fills them with some test values, and passes them to the Evaluator<TSource> class for final evaluation against our rule:

using CodeEffects.Rule.Core;
using TestLibrary;

namespace TestProgramm
	public class Executor
		static void Main(object[] args)
			// Load the rule from database
			string ruleXml = YourDataModel.GetRuleXml();

			// Create the first instance of the source object
			Person person = new Person();
			person.FirstName = "John";
			person.MiddleName = "James";
			person.LastName = "Doe";

			// Evaluate the rule against the first instance
			Evaluator<Person> evaluator = new Evaluator<Person>(ruleXml);

			// Second instance
			person = new Person();
			person.FirstName = "John";
			person.MiddleName = "James";
			person.LastName = "Smith";

			// Evaluate the rule against the second instance.
			// We can reuse the existing evaluator because
			// both evaluations use the same rule.

It's easy to see that ActionOne will be called at the end of the first evaluation, and ActionTwo at the end of the second evaluation. The Evaluator makes those calls by invoking action methods. Read more about action methods, external actions, in-rule methods, and other features of Code Effects in the source object topic.

Post your questions on Stackoverflow and become a part of our growing community

Comments: 0