Registering Functions
Have your MVC functions registered on the application startup
For C1 CMS to recognize your MVC controllers and actions as regular CMS Functions, you should register them in the system. For this, use a class with the ApplicationStartup
attribute.
(This class should have two public static methods: OnBeforeInitialize()
and OnInitialized()
. The ApplicationStartup
attribute has been defined in Composite.Core.Application
. Please see ApplicationStartup for the information about using this attribute.)
First, in your website project:
- Create a static class with the
ApplicationStartup
attribute. - Add two static methods:
OnBeforeInitialize()
andOnInitialized()
.
using Composite.Core.Application; namespace DemoMVC { [ApplicationStartup] internal static class StartupHandler { public static void OnBeforeInitialize() { } public static void OnInitialized() { } } }
Next, to register your functions, in the OnBeforeInitialize()
method, you should:
- get a new function collection:
var functions = MvcFunctionRegistry.NewFunctionCollection();
- register your controllers and/or action (see Registering MVC Controllers and Registering MVC actions below)
- and also map routes into it:
functions.RouteCollection.MapMvcAttributeRoutes(); functions.RouteCollection.MapRoute( "Default", "{controller}/{action}/{id}", new { action = "Index", id = UrlParameter.Optional } );
(As MvcFunctionRegistry
has been defined in Composite.AspNet.MvcFunctions
, be sure to add the reference to it in your website project.)
Consider the following options in registering MVC controllers/actions and defining parameters.
Registering MVC controllers
To register your controllers, you have two basic approaches:
- In which you don’t need to change your ASP.NET MVC application code.
- In which you want your controllers to be automatically discovered.
Approach 1. No MVC code change
In this approach you don’t need to change your ASP.NET MVC application code.
Register your controller in the OnBeforeInitialize()
method:
functions.RegisterController<YourController>("Namespace.FunctionName");
Here:
- YourController is your controller’s class name
- Namespace is the name you want to use as namespace in your C1 CMS
- FunctionName is the name you want to use as your function in your C1 CMS
Approach 2. Controllers getting auto-discovered
In this approach you want your controllers to be automatically discovered.
First, indicate it in the the OnBeforeInitialize()
method:
functions.AutoDiscoverFunctions(typeof(AutoDiscoverController).Assembly);
Here:
- Just refer the assembly you want to be searched.
Then, decorate your controller in your ASP.NET MVC application with the [C1Function]
and the Namespace and Function name that you want to use.
Your class should be public, inherited from the Controller
class, and have the Controller
suffix:
[C1Function(Namespace = "Namespace",Name = "FunctionName")] public class AutoDiscoverController : Controller
Registering MVC actions and defining parameters
You can also register your actions and define parameters.
You have two approaches here as well:
- In which you don’t need to change your ASP.NET MVC application code.
- In which you want your actions to be automatically discovered.
Approach 1. No MVC code change
In this approach you don’t need to change your ASP.NET MVC application code.
Register your action in the OnBeforeInitialize()
method:
functionCollection.RegisterAction<YourController>("YourActionName", "Namespace.FunctionName").AddParameter("ParameterName", label: "Label");
Here:
- YourController is your controller’s class name
- YourActionName is the name of the action in the controller that is receiving parameters.
- Namespace is the name you want to use as a namespace in your C1 CMS
- FunctionName is the name you want to use as your function in your C1 CMS
- ParameterName should be the exact name of the parameter in your action
- Label is the text you want to be represented in as the Label for this parameter in C1 CMS
Approach 2. Actions getting auto-discovered
In this approach you want your actions be automatically discovered.
First, register your action in the OnBeforeInitialize()
method:
functionCollection.RegisterController<YourController>("Namespace.FunctionName").AddParameter("ParameterName", label: "Label").AddParameter("AnotherParameterName", label: "AnotherLabel");
Here:
- all the parameters are the same as above
Then, decorate your desired action in your ASP.NET MVC application with:
[Route("YourControllerNameWithoutSuffix/{ParameterName}/{AnotherParameterName}")] public ActionResult Index(int ParameterName = 5, string AnotherParameterName = "Whatever")