How can I modify the route of a url instead of using a query string?
I have a simple blog page on my site, sitting under the root at /blog
All my blog posts have the Tags property on them, which I'm utilizing to list out all the tags that the blog posts are marked with. What I'm trying to do is have each tag link to the blog list page, and then I'll only show blog posts associated with that tag.
I could just pass it through the url as a query string by linking to /blog?tag=myTagName, and then doing the filtering from there.
But I'd like to link to /blog/tag/myTagName, I just think it's cleaner. I'm not sure how I can modify the routing though, or where to even start.
I use the term "category" but there are also tags. I do mostly want you are trying to achieve in that demo project. Maybe have a look and if you have questions feel free to ask them.
So you can keep your code working with query string params, but just change your links to use the format you want. You'll see your "nice" URL in the browser, but in actuality IIS will be passing the query string param as before.
The downside is that you'd have to generate those tag links yourself - Umbraco won't do it for you, but it should be pretty simple.
How can I modify the route of a url instead of using a query string?
I have a simple blog page on my site, sitting under the root at /blog
All my blog posts have the Tags property on them, which I'm utilizing to list out all the tags that the blog posts are marked with. What I'm trying to do is have each tag link to the blog list page, and then I'll only show blog posts associated with that tag.
I could just pass it through the url as a query string by linking to /blog?tag=myTagName, and then doing the filtering from there.
But I'd like to link to /blog/tag/myTagName, I just think it's cleaner. I'm not sure how I can modify the routing though, or where to even start.
Could someone lead me in the right direction?
you have a couple of options to generate the url, based on what you have in cms
if the tags are actually content nodes in the tree, you could use a IUrlProvider implementation to generate a url /blog/tag/tag-name
See https://our.umbraco.org/Documentation/Reference/Routing/Request-Pipeline/outbound-pipeline
Otherwise, just generate the urls yourself based on the url of the blog page and append suffix /tag/tag-name
And to get from the url and fetch the information you want, you may want to look into IContentFinder implementations
See https://our.umbraco.org/Documentation/Reference/Routing/Request-Pipeline/IContentFinder
Or, if you're already into mvc and routing, and you're sure /blog/tag is going to be fixed, have a look at
https://our.umbraco.org/Documentation/Reference/Routing/custom-routes
Hope you find what you're looking for.
--Dirk
Thanks Dirk, the custom routes was what I was looking for!
Hi Steven,
as you stated that you are using the tags property I guess you need to do some custom MVC stuff to get the tags in a url to work.
There is the possibility to use register custom routes and map them to a controller like in normal MVC.
This looks like this: https://github.com/Mantus667/UmbracoUrlHandling/blob/master/src/UmbracoUrlHandling/Startup.cs#L67
I use the term "category" but there are also tags. I do mostly want you are trying to achieve in that demo project. Maybe have a look and if you have questions feel free to ask them.
Regards David
There's also a much simpler way you can do this, which might not be quite as neat, but is quicker and easier. You can use a IIS URL rewrite.
So, if you add something like:
So that will rewrite:
to
So you can keep your code working with query string params, but just change your links to use the format you want. You'll see your "nice" URL in the browser, but in actuality IIS will be passing the query string param as before.
The downside is that you'd have to generate those tag links yourself - Umbraco won't do it for you, but it should be pretty simple.
is working on a reply...