Copied to clipboard

Flag this post as spam?

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


  • Bear 40 posts 129 karma points
    Sep 19, 2013 @ 21:34
    Bear
    0

    Managing MVC Urls

    I want to break out of the Umbraco /folder/node scenario. It's too restrictive. MVC & IUrlSegmentProvider the provides the perfect way for me to take back some control of Urls, but I need to understand how I can leverage this in Umbraco.

    The problem, here is my folder structure. This is perfect for my content editors.

    - The World
      - Country
        - Region
    

    Umbraco would treat these urls as

    http://www.my-site.com/The-World/
    http://www.my-site.com/The-World/Country/
    http://www.my-site.com/The-World/Country/Region/
    

    I know I can take control of these in various ways, particularly using the IUrlSegmentProvider. Can start introducing node id's into the url;

    I could do something like <node id>-<node name>;

    http://www.my-site.com/The-World/1234-Country/

    And look for the node id for fast a faster lookup.

    This works great, but ideally I want to manipulate the Url but avoid a url like

    http://www.my-site.com/The-World/1234-Country/5678-Region

    How would I create a cleaner url that my users can guess and that can be assigned to the .Url parameter in Umbraco?

    http://www.my-site.com/Country/Region

    Whilst still retaining the "The-World" folder where my content editors know to look.

  • Stephen 767 posts 2273 karma points c-trib
    Sep 19, 2013 @ 21:47
    Stephen
    0

    So you want to remove "The-World" from your urls, right?

    Outbound: you want a IUrlProvider (not a IUrlSegmentProvider) that modifies the whole Url by removing its first segment.

    Inbound: because you modify the whole url and not only a segment, you need a content finder to map that url to a node... you probably could inherit from the default "finder by url" and check for ("The-World" + request)...Just add it after the default "finder by url". That should do it.

    See what I mean, or do you need more details?

    I'll be offline soon, and 'till monday, so don't be surprised if I stop responding, though.

    Stephan

  • Bear 40 posts 129 karma points
    Sep 19, 2013 @ 21:54
    Bear
    0

    Ah, another thing to look into... the IUrlProvider. I'll check the source.

    Any quick examples would be appreciated. I can think of a few more example that break this scenario. :-)

  • Bear 40 posts 129 karma points
    Sep 24, 2013 @ 11:38
    Bear
    0

    I did have a play with this and looked at the source. I could modify the the Url and remove the "The World" from the Url by inheriting from the default "finder by url", but I got a bit lost with the source (it does all sorts of stuff with Urls, Domains, Absolute and Relative Paths).

  • Stephen 767 posts 2273 karma points c-trib
    Sep 24, 2013 @ 11:47
    Stephen
    100

    The following finder should add your prefix back:

    public class CustomFinder : ContentFinderByNiceUrl
    {
      public override bool TryFindContent(PublishedContentRequest request)
      {
        // copy ContentFinderByNiceUrl but add your prefix back where it's missing
        const string PREFIX = "/The-World";

        string route;
        if (docRequest.HasDomain)
          route = docRequest.Domain.RootNodeId.ToString() + PREFIX + DomainHelper.PathRelativeToDomain(docRequest.DomainUri,
            docRequest.Uri.GetAbsolutePathDecoded());
          else
            route = PREFIX  + docRequest.Uri.GetAbsolutePathDecoded();

        var node = FindContent(request, route);
        return node != null;
      }
    }

    Then you need to register the finder. Know how to do it?

    Have you been able to work out the url provider?

    Stephan

  • Bear 40 posts 129 karma points
    Sep 24, 2013 @ 12:20
    Bear
    0

    Thank you for this. I can register the finder... but do I add it after the default one or before?

    I did try to figure out how to remove it, but it was a bit "brute force" (just used .Replace("/The-World","") on the route. Could you show me how you'd do it? (Keen to learn the right way!)

  • Stephen 767 posts 2273 karma points c-trib
    Sep 24, 2013 @ 12:48
    Stephen
    0

    You add it in place of the default one... so I'd insert it before ContentFinderByNiceUrl, then remove ContentFinderByNiceUrl.

    As for building the url... there could be a nicer way than .Replace() but I'm not sure it'll work because of some stuff that are private/internal. So probably the best is:

    public class MyUrlProvider : DefaultUrlProvider
    {
      public override string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
      {
        return base.GetUrl(umbracoContext, id, current, mode).Replace("/The-World", "");
      }
    }

    And register it in place of the default url provider.

    Stephan

  • Bear 40 posts 129 karma points
    Sep 25, 2013 @ 17:58
    Bear
    0

    Thank you so much for these examples. So much good stuff to play with in 6! :)

  • Stephen 767 posts 2273 karma points c-trib
    Sep 25, 2013 @ 18:06
    Stephen
    0

    No prob. And don't hesitate to explain what you're about to do and ask for opinion: because many things have changed, it's always better to ask before spending hours re-inventing something that's just poorly documented (yes, I suck at writing docs).

  • John French 32 posts 83 karma points
    Jul 23, 2014 @ 03:28
    John French
    0

    Hello guys,

     

    I need to do something similar and just looking at this post - some useful stuff - @Stephen - how do I register my own urlprovider in place of the default one?

    Thanks

  • Stephen 767 posts 2273 karma points c-trib
    Jul 23, 2014 @ 07:41
    Stephen
    0

    @James: you need to create an ApplicationEventHandler class and override the starting method, and in there...

    UrlProviderResolver.Current.Add<YourProvider>()

    You can also insert, insert before, etc.

    Making sense?

  • John French 32 posts 83 karma points
    Jul 23, 2014 @ 08:03
    John French
    0

    ah yeah of course! thanks!

Please Sign in or register to post replies

Write your reply to:

Draft