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?
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.
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.
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 anIndex
action which returns a list of posts (BlogPost document types) to the route~/blog
.I also have a
BlogPostController
with anIndex
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 theIndex
(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?
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
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 fromRenderController
for route hijacking.The two options documented in the updated documentation mention that the controller can't inherit from
RenderController
.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
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!
is working on a reply...