Copied to clipboard

Flag this post as spam?

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


  • Bas Hessels 1 post 21 karma points
    Feb 02, 2021 @ 11:10
    Bas Hessels
    0

    Routing, first umbraco, then to custom controllers

    I've created my own controller like this:

    public class TestController : Umbraco.Web.Mvc.SurfaceController
    {
        [Route("{categoryAlias}/{productAlias}")]
        public ActionResult Index(string categoryAlias, string productAlias)
        {
            return Content($"Category: {categoryAlias} - productAlias: {productAlias}");
        }
    }
    

    And implemented an IComponent like this

        public class RegisterCustomRouteComposer : ComponentComposer<RegisterCustomRouteComponent>
        {
    
        }
    
        public class RegisterCustomRouteComponent : IComponent
        {
            public void Initialize()
            {
                RouteTable.Routes.MapMvcAttributeRoutes();
            }
    
            public void Terminate()
            {
                throw new NotImplementedException();
            }
        }
    

    This works perfectly because when I call the path /category1/productname1 it'll return

    Category: category1 - productAlias: productname1

    Problem is that now it'll also choose my custom controller even if I go to /contact/email which is an existing umbraco page.

    This means I am hijacking ALL of the calls to anything with 2 levels of nodes (/anything/anything).

    Is there a way to make it first check if there is an existing umbraco node/path and if not then to defer to any custom MVC controllers willing to take the request. Now it seems to be the other way around.

Please Sign in or register to post replies

Write your reply to:

Draft