Search code examples
c#asp.net-mvcumbracoumbraco8

How to create normal controllers and views in an ASP.NET MVC application that already has umbraco 8 installed


I would like to include normal controllers and views in my ASP.NET MVC application that already has Umbraco setup, pardon me if I am missing something as I am new to Umbraco.

I tried to follow this https://24days.in/umbraco-cms/2016/adding-umbraco-to-existing-site/ but its based on Umbraco 7 and I am unable to inherit from IApplicationEventHandler.

I have tried to add controller and views directly but the routing doesn't work, as Umbraco takeover the routing.

I would like to know how to create normal ASP.NET MVC controllers, views as well as their routing in Umbraco. TIA


Solution

  • There is no IApplicationEventHandler in Umbraco8, they have replaced it with User Composers

    Umbraco has its own global.asax implementation and as you said it overwrites the default routings. The usual routing class is not executed, you have to add your routings when the application starts.

    I managed to do that with creating a User Composer. User composers compose after core composers, and before the final composer.

    (Below, I create an IComposer, but IUserComposer should also work.)

    public class ApplicationEventComposer : IComposer
    {
        public void Compose(Composition composition)
        {
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }
    }
    

    In this one you can register your own RouteConfig, Bundles, etc. Just be careful, it is easy to mess up the Umbraco routings...

    Here is an example to add a new controller called TestController:

    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.MapRoute(
                name: "Test",
                url: "Test/{action}/{id}",
                defaults: new { controller = "Test", action = "Index", id = UrlParameter.Optional }
            );
        }
    }