Copied to clipboard

Flag this post as spam?

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


  • Steven Applegate 18 posts 178 karma points
    Dec 15, 2017 @ 04:08
    Steven Applegate
    0

    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?

  • Dirk De Grave 4541 posts 6021 karma points MVP 3x admin c-trib
    Dec 15, 2017 @ 07:42
    Dirk De Grave
    101

    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

  • Steven Applegate 18 posts 178 karma points
    Dec 16, 2017 @ 03:43
    Steven Applegate
    0

    Thanks Dirk, the custom routes was what I was looking for!

  • David Brendel 792 posts 2970 karma points MVP 3x c-trib
    Dec 15, 2017 @ 07:50
    David Brendel
    2

    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

  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Dec 15, 2017 @ 09:11
    Dan Diplo
    0

    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:

        <rule name="Blog Tag" stopProcessing="true">
          <match url="^(.*)/tag/(.+)/$" />
          <action type="Rewrite" url="{R:1}/?tag={R:2}" />
        </rule>
    

    So that will rewrite:

    /anything/you/want/blog/tag/mytagname/
    

    to

    /anything/you/want/blog/tag?tag=mytagname
    

    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.

Please Sign in or register to post replies

Write your reply to:

Draft