Copied to clipboard

Flag this post as spam?

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


  • Adriano Fabri 459 posts 1602 karma points
    Feb 20, 2018 @ 09:40
    Adriano Fabri
    0

    My custom route doesn't display anything

    Hi to all, I'm trying to create and view my Custom Route to view some external data. I wrote this code following the official umbraco documentation but it don't work.

        public class HkRegisterEvents : ApplicationEventHandler
        {
            protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
            {
                RouteTable.Routes.MapUmbracoRoute(
                    name: "HKRecycleBin",
                    url: "HKRecycleBin/{action}",
                    defaults: new
                    {
                        controller = "HKRecycleBin",
                        action = "Index"
                    },
                    new HKRouteHandler(_pageNodeId));
            }
        }
    
        public class HKRecycleBinController : RenderMvcController
        {
            public ActionResult Index(RenderModel model)
            {
                return View("HK/RecycleBin.cshtml", model);
            }
        }
    
        public class HKRouteHandler : UmbracoVirtualNodeRouteHandler
        {
            private object _pageNodeId;
    
            public HKRouteHandler(object pageNodeId)
            {
                _pageNodeId = pageNodeId;
            }
    
            protected override IPublishedContent FindContent(RequestContext requestContext, UmbracoContext umbracoContext)
            {
                UmbracoHelper helper = new UmbracoHelper(umbracoContext);
                string alias = typeof(HKRecycleBinController).Name;
                return helper.TypedContentAtRoot()
                    .First()
                    .Descendants()
                    .First(d => d.DocumentTypeAlias.InvariantEquals(alias));
            }
        }
    

    I think that the problem is that there isn't a real node in Umbraco and when I try to view my custom url "http://localhost:7612/HkRecycleBin/Index", I don't see nothing and in the Trace Log I see an "Umbraco has no content" Warning.

    I tried also to inherits my controller from SurfaceController...but nothing.

    This is a service page and I wouldn't create a specific node in the content.

    Adriano

  • Adriano Fabri 459 posts 1602 karma points
    Feb 20, 2018 @ 15:13
    Adriano Fabri
    0

    OK...at the moment I solved using the umbraco route hijacking method...so I created my custom datatype, template and node used by my custom controller.

    It solve the problem at this moment but this is not what I want.

    My target is to remove my custom umbraco node in the content and use only my custom view as a external page.

    Can anyone help me? Thank you in advance

  • Roy van den Ekker 14 posts 84 karma points
    Feb 21, 2018 @ 09:18
    Roy van den Ekker
    0

    To start I see you are not really following the documentation. ;)

    Documentation says ApplicationStarted, not ApplicationStarting.

    And I guess it your case you can drop all pageNodeId references but possibly this is just some sample code.

    {action} parameter is for future coding?

    (all just to think together with you, I know I am not actually solving the issue)

  • Adriano Fabri 459 posts 1602 karma points
    Feb 21, 2018 @ 13:56
    Adriano Fabri
    0

    You're right.

    1. ApplicationStarting is a last attempt I tried (I copy/paste the wrong code). I used correctly "ApplicationStarted" as Documentation says.
    2. Yes...{action} is a parameter for further coding
  • Roy van den Ekker 14 posts 84 karma points
    Feb 21, 2018 @ 14:03
    Roy van den Ekker
    0

    Think the FindContent should actually be able to find content somewhere in the content tree. And your HKRecycleBinController will not give an alias for a document type.

    For fake content, not actually having an entry in the tree (what you want right?), just create a route to an action in a surface controller. And let that action output your stuff.

    No need for a custom route handler.

  • Adriano Fabri 459 posts 1602 karma points
    Feb 27, 2018 @ 08:45
    Adriano Fabri
    0

    Thank you Roy,

    I already tried to inherits from SurfaceController instead of RenderMvcController (and removed the HKRouteHandler), but it didn't work.

    Surely I did something wrong I'll try again Thanks again for the support

Please Sign in or register to post replies

Write your reply to:

Draft