Loading custom classes from App_Code

By preparing custom classes that inherit from an appropriate base class, you can extend the functionality of Kentico. This approach allows you to implement the following types of objects:

If you create the custom classes in your project’s App_Code folder (or Old_App_Code on web application installations), you do not need to integrate a new assembly into the web project. Code in the App_Code folder is compiled dynamically and automatically referenced in all other parts of the system.

Registering custom classes in the App_Code folder

To ensure that the system can load custom classes placed in App_Code, you need to register each class.

  1. Edit your custom class.
  2. Add a using statement for the CMS namespace.
  3. Add the RegisterCustomClass assembly attribute above the class declaration (for every App_Code class that you want to register).



using CMS;

// Ensures that the system loads an instance of 'CustomClass' when the 'MyClassName' class name is requested.
[assembly: RegisterCustomClass("MyClassName", typeof(CustomClass))]

...

public class CustomClass
{
    ...
}


The RegisterCustomClass attribute accepts two parameters:

  • The first parameter is a string identifier representing the name of the class.
  • The second parameter specifies the type of the class as a System.Type object. When the system requests a class whose name matches the first parameter, the attribute ensures that an instance of the given class is provided.

Once you have registered your custom classes, you can use them as the source for objects in the system. When assigning App_Code classes to objects in the administration interface, fill in the following values:

  • Assembly name: (custom_classes)
  • Class: must match the value specified in the first parameter of the corresponding RegisterCustomClass attribute

Assigning a custom App_Code class to a new translation service