Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dibs 202 posts 991 karma points
    May 30, 2019 @ 15:05
    Dibs
    0

    Custom MVC Routes error

    Dear Umbraco Team,

    I have created a Custom MVC route, via this article. When i build and run application via Visual Studio i get the YSOD with the following error

    Unable to resolve type: dummy.Controllers.HomeController, service name:
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
    
    Exception Details: System.InvalidOperationException: Unable to resolve type: dummy.Controllers.HomeController, service name: 
    

    here is my code

    using System;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Umbraco.Core.Composing;
    using Umbraco.Web;
    
    namespace dummy.Components
    {
        public class RegisterCustomRouteComposer : ComponentComposer<RegisterCustomRouteComponent>
        {
    
        }
    
        public class RegisterCustomRouteComponent : IComponent
        {
            public void Initialize()
            {
                RouteTable.Routes.MapRoute("Home", "{controller}/{action}/{id}", new
                {
                    controller = "Home",
                    action = "Index",
                    id = UrlParameter.Optional
                });
            }
    
            public void Terminate()
            {
                throw new NotImplementedException();
            }
        }
    }
    

    My controller class is

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace dummy.Controllers
    {
        public class HomeController : Controller 
        {
            public ActionResult Index()
            {
                return View();
            }
        }
    }
    

    What have i done wrong ?

    Thanks Dibs

  • Marc Goodson 2138 posts 14321 karma points MVP 8x c-trib
    May 30, 2019 @ 22:55
    Marc Goodson
    101

    Hi Dibs

    It depends a little on what you are trying to do as to what the best advice to offer is...

    However the error you are seeing is because your HomeController only inherits from Controller, and therefore is not automatically registered with Umbraco's LightInject dependency injection container.

    Therefore you can make the error go away, by registerring your HomeController explicitly via a UserComposer eg:

    using Umbraco.Core;
    using Umbraco.Core.Composing;
    using Umbraco.Web.Routing;
    using Umbraco8.Controllers;
    using Umbraco8.Routing;
    using Umbraco8.Services;
    
    namespace Umbraco8.Composers
    {
        public class RegisterHomeControllerComposer : IUserComposer
        {      
            public void Compose(Composition composition)
            {            
                composition.Register<HomeController>(Lifetime.Request);         
            }
        }
    }
    

    Then your boot error will go away.

    If your aiming to use your custom controller with the Umbraco pipeline however - then no need to do this, as you should make your HomeController inherit from Umbraco.Web.Mvc.RenderMvcController then it will be automatically added to the LightInject container...

    namespace Umbraco8.Controllers
    {
        public class HomeController : RenderMvcController
        {
            // GET: Home
            public ActionResult Index()
            {
                return View();
            }
        }
    }
    

    If you are mapping an MVC route that you want to associate with Umbraco templates - eg have an underlying associated implementation of PublishedContent with your request then consider using MapUmbracoRoute instead eg

    public class RegisterCustomRouteComposer : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.Components().Append<RegisterCustomRouteComponent>();
        }
    }
    
    public class RegisterCustomRouteComponent : IComponent
    {
    
    
        public void Initialize()
        {
            GlobalConfiguration.Configuration.MapHttpAttributeRoutes();
    
            RouteTable.Routes.MapUmbracoRoute("HomeRoute", "home/{action}/{id}", new
            {
                controller = "Home",
                id = UrlParameter.Optional
            }, new UmbracoVirtualNodeByIdRouteHandler(1105));
        }
    
        public void Terminate()
        {
            throw new NotImplementedException();
        }
    }
    

    }

    (you can implement your own logic for UmbracoVirtualNodeHandler to construct the published content to associate to the request)

    If you just want to 'hijack' requests to the home page of your site, then you could create a RenderMvcController with a name matching the alias of the document type of your homepage, and this would automagically be routed for the request:

        public class HomePageController : Umbraco.Web.Mvc.RenderMvcController
       public override ActionResult Index(ContentModel model)
            {
    
    return CurrentTemplate(model);
    }
    }
    

    So knowing what you are trying to achieve would define what is the best approach - but at least the first bit of this reply explains the error you see!

    regards

    Marc

  • Hugo Becerra 12 posts 82 karma points
    Sep 11, 2019 @ 13:54
    Hugo Becerra
    0

    Hi Mark,

    Could you please check the problem that we have? I tried everything but still having an error. The only way that I found was using Url Rewriting.

    https://our.umbraco.com/forum/umbraco-8/99009-umbraco-v8-custom-routing-for-umbracoapicontroller

  • Dibs 202 posts 991 karma points
    Jun 05, 2019 @ 14:33
    Dibs
    0

    Hi Mark

    Thanks for the reply : )

    I was able to get rid of the error by my controller inheriting from RenderMvcController.

    I was able to map MVC route with UmbracoVirtualNodeByRouterHandler(1111), how can i use this by using the template name instead of an ID ?

    Thanks Dibs

Please Sign in or register to post replies

Write your reply to:

Draft