Copied to clipboard

Flag this post as spam?

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


  • Dekker 13 posts 33 karma points
    Sep 17, 2010 @ 21:54
    Dekker
    0

    URL Encoding Inconsistency

    Maybe I'm not seeing something obvious, but I'm having a difficult time encoding a URL.

    When you create a new content page, with a name with SPACES and/or special characters like COMMAS, those spaces are replaced by DASHES in the final URL when the page is published.

    Therefore, if you create a new page with a title of "The Umbraco Theory, of Everything", when you publish it becomes "/the-umbraco-theory,-of-everything.aspx". No problem so far. Nice and predictable (but is comma legal?)

    But say you create a template that takes a string Textstring and tries to convert that Textstring to a URL, you get something completely different! The results you get are as follows:

    for a given alias of "The Umbraco Theory, of Everything", you get:
    
    <umbraco:item field="alias" runat="server" case="lower" urlencode="true" /> 
    produces->  the+umbraco+theory%2c+of+everything
    
    <umbraco:item field="alias" runat="server" case="lower" htmlencode="true" />
    produces->  the umbraco theory, of everything

    Obviously, something is not correct, but I don't know which, and I don't know how to solve the real problem: How to turn an arbitrary string into the same formatting as generated by Umbraco for page URLs when published.

    Any ideas?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Sep 17, 2010 @ 22:11
    Tom Fulton
    0

    The URLEncode is just converting the string into the correct/encoded syntax for URLs, it's doesn't have any reference to the way Umbraco creates it's page URLs.

    I don't think there is a good way to get an Umbraco URL from a string - but I'm not sure why you would want to either?  Umbraco has a built in function umbraco.library:NiceUrl to get a page's URL from it's node ID.  What exactly are you wanting to use this approach for?

    BTW - the way Umbraco replaces spaces/etc in URLs is configurable in /config/umbracoSettings.config in the urlReplacing section

  • Dekker 13 posts 33 karma points
    Sep 18, 2010 @ 02:44
    Dekker
    0

    Thanks Tom... Still hoping for a nice method to URLencode from a string into the same format as Umbraco.

    The reason is that I have about 300 pages that were generated pseudo-automatically and inserted into Umbraco. Of course, they used the standard Umbraco naming convention, of spaces-to-dashes. I also have a few hundred pages that have a snippet of text, the same text that was used for the title of the original 300. I had thought there would be a way to simply encode the string into the same format, and just surround it by an href block.

    Unless anyone else comes up with a different idea, I'll go with your suggestion and just do a "search" for the NodeID, but that seems odd since I already have the page's title, just need to format it.

    I'll have to check out that urlReplacing block too.

    Dekker

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Sep 18, 2010 @ 02:58
    Tom Fulton
    0

    The only way I can think of is to use some Replace logic to replace all the characters Umbraco does when generating the page name.  You can see them in the urlReplacing block.  Something like:

    <umbraco:Item field="alias" runat="server" Xslt="umbraco.library:Replace(umbraco.library:Replace({0},' ','-'), '!', ''"></umbraco:Item>

    ...as a quick solution.  You'd have to wrap a bunch of Replace statements to handle every possible case, or probably can use some .NET to do it more efficient.

    You'd still be "guessing" at the name though.  And if the name of the target page changes, it'd no longer work. 

    Of course, the "correct" way would probably be to add a Content Picker on the pages and select the target page to link to on each, and use NiceUrl to add the link.  But I imagine you are trying to avoid having to do that manually :)

  • Dekker 13 posts 33 karma points
    Sep 27, 2010 @ 15:29
    Dekker
    0

    Just to close my own post... the solution that I settled on was as follows.

      <xsl:variable name="newId" select="$currentPage/../SpeciesDetails[@nodeName = $currentPage/alias]/@id"/>
      <xsl:variable name="link" select="umbraco.library:NiceUrl($newId)"/>
      <xsl:value-of select="$link"/>
    

    Essentially, I wanted to select a page based on a text string on the current page (it is actually a species scientific name, hence unique). So I use XSLT to filter the pages for a @nodeName=$currentPage/alias, and this gives me the @id that I want. I then use NiceUrl($newId) to give me the actual link to the destination.

    Thanks Tom for starting me on the right direction!

     

Please Sign in or register to post replies

Write your reply to:

Draft