Hello World MVC

Register the controller as a CMS function

Now you need to register the Home controller on the website as an MVC function.

  1. On your website, add a class to ~/App_Code called “Startup”: ~/App_Code/Startup.cs. (Create the folder if it is not there.)
  2. Replace its content generated by default with the following code:
    using Composite.AspNet.MvcFunctions;
    using Composite.Core.Application;
    using System.Web.Mvc;
     
    namespace DemoMVC
    {
        [ApplicationStartup]
        internal static class StartupHandler
        {
            public static void OnBeforeInitialize()
            {
                var functions = MvcFunctionRegistry.NewFunctionCollection();
                functions.RegisterController<Composite.Samples.SimpleMvc.Controllers.HomeController>("Test.SimpleMvc.Home");
     
                functions.RouteCollection.MapMvcAttributeRoutes();
                functions.RouteCollection.MapRoute(
                    "Default",
                    "{controller}/{action}/{id}",
                    new { action = "Index", id = UrlParameter.Optional }
                    );
            }
     
            public static void OnInitialized()
            {
     
            }
        }
    }
  3. Save the file.
  4. Restart the server.

There are different ways of registering controllers and actions from you MVC application as CMS functions. Please see ”Registering Functions” for more infromation.

Alternatively, you may want to have the startup code that registers your MVC functions in a project, build it and copy the output DLL in ~/Bin rather than creating the class in ~/App_Code.