Copied to clipboard

Flag this post as spam?

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


  • Tom Jones 19 posts 150 karma points
    Jan 02, 2023 @ 19:45
    Tom Jones
    0

    Edit route for each blog post

    Hi folks, I'm new to Umbraco (but not to .NET or MVC) and I'm wondering what the best approach to adding a subdirectory to the controller routing would be.

    I have a Blog document type which uses a BlogController, with an Index action which returns a list of posts (BlogPost document types) to the route ~/blog.

    I also have a BlogPostController with an Index action which returns the content of a particular post, with each post currently navigating to ~/post-name, however I want each of these posts to be found at ~/blog/post-name.

    In standard MVC I would simply have a BlogController which has two actions, one for the Index (the list view), and another for an individual blog page with a [Route("{controller}/post-name")] attribute over the action, but with Umbraco I presume the document types need to have their own controllers due to standard naming/routing conventions? Please correct me if I'm mistaken.

    Can this be achieved by changing something in the back office (or on the controllers/Startup.cs), rather than using URL rewrites/redirects?

  • Lewis Smith 211 posts 620 karma points c-trib
    Jan 03, 2023 @ 09:32
    Lewis Smith
    0

    Hi Tom,

    Welcome to the world of Umbraco!

    You should be able to achieve this with umbraco custom routes, see: https://our.umbraco.com/documentation/reference/routing/custom-routes/

    On Initialize you run MapUmbracoRoute.

    You can check out the "real world example" - this covers you for a dynamic URL, so if a user were to change the url of the blogs folder as an example.

    Lewis

  • Tom Jones 19 posts 150 karma points
    Jan 06, 2023 @ 15:06
    Tom Jones
    0

    Hi Lewis, thanks for the reply.

    This documentation refers to v7-8 and mentions using a RenderMvcController, I'm using v10 and my controllers inherit from RenderController for route hijacking.

    The two options documented in the updated documentation mention that the controller can't inherit from RenderController.

  • Yakov Lebski 594 posts 2350 karma points
    Jan 06, 2023 @ 19:21
    Yakov Lebski
    1

    Umbraco should control URLs for posts and any pages in CMS. by default, Umbraco build url hierarchy

    root = "/"

    -page = "/page"

    --subpage1 ="/page/subpage1"

    you also can control url if you need it

  • Tom Jones 19 posts 150 karma points
    Jan 06, 2023 @ 20:23
    Tom Jones
    100

    Your reply led me to check the back office document type/content hierarchy - Blog was at the same level as Home (root), so BlogPost items were also being created at the root, rather than under Blog.

    So I recreated Blog as a child of Home, then added a BlogPost, making the route ~/blog/post-name.

    Thanks!

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies