Copied to clipboard

Flag this post as spam?

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


  • Keith Jackson 183 posts 552 karma points
    Feb 09, 2013 @ 01:41
    Keith Jackson
    1

    Umbraco 6 - New API - How do I get a URL?

    I'm having a play with the new Services centric API, which looks really sweet and, using my existing abstractions I should be able to switch out quite easilly.

    I'm a bit stumped though on how I get a Url in the new API - I can't seem to find anything resembling a NiceUrl() or Url() method or property anyway. Should I be using 'Path'?

    Here's an example...

            /// 
    /// Gets the content URL.
    ///
    /// The node id.
    /// A nicely formed Url.
    public string ContentUrl(int nodeId)
    {
    return Services.ContentService.GetPublishedVersion(nodeId).Path;
    }

    Is this what I need? I used to have...

            /// 
    /// Gets the content URL.
    ///
    /// The node id.
    /// A nicely formed Url.
    public string ContentUrl(int nodeId)
    {
    return Umbraco.Content(nodeId).NiceUrl();
    }

     

  • Pinal Bhatt 298 posts 390 karma points
    Feb 11, 2013 @ 18:01
    Pinal Bhatt
    0

    yes me too looking for how to get URL from IContent object using new APIs for Umbraco 6? (http://our.umbraco.org/forum/developers/api-questions/38307-How-to-get-NiceUrl-from-IContent-object-in-Umbraco-6)

  • Keith Jackson 183 posts 552 karma points
    Feb 13, 2013 @ 11:53
    Keith Jackson
    0

    Pinal - did you get anything on this?

  • Pinal Bhatt 298 posts 390 karma points
    Feb 13, 2013 @ 12:30
    Pinal Bhatt
    0

    Not as such. I have big lot of issues(http://our.umbraco.org/forum/developers/api-questions/38322-Issues-with-new-Umbraco-6-APIs) with new APIs in U6

    But I have my workaround. Using legacy  umbraco.library.NiceUrl(int) . Not sure if new APIs have any good solution for that and also not sure how good or bad this method is from performance point of view.

    umbraco.library.NiceUrl(IContent.Id)
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 09, 2013 @ 11:04
    Sebastiaan Janssen
    1

    Remember, you should only use the Services APIs if you need to do tasks on unpublished content. For simple querying in your frontend you should never use the ContentService or MediaService as they always go directly to the database which is not good for your performance.

    In macroscripts you could do something like:

    @using Umbraco.Web
    @{
        var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    }
    
    @umbracoHelper.NiceUrl(yourId)
    

    In a usercontrol you can inherit from UmbracoUserControl:

    using System;
    using Umbraco.Web.UI.Controls;
    
    namespace YourNamespace
    {
        public class YourUserControl : UmbracoUserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                Umbraco.NiceUrl(yourNodeId);
                //etc
            }
        }
    }
    

    In a SurfaceController:

    using System.Linq;
    using System.Web.Mvc;
    using Umbraco.Web.Mvc;
    
    namespace YourNamespace
    {
        public class YourController : SurfaceController
        {
            [HttpPost]
            public ActionResult HandleSomething(YourModel model)
            {
                Umbraco.NiceUrl(yourNodeId);
                //etc.
    
            }
        }
    }
    

    Apart from NiceUrl there's also NiceUrlWithDomain to get everything.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Sep 09, 2013 @ 18:00
    Sebastiaan Janssen
    1

    Oh I forgot:

    In MVC Razor views you can just do:

    @Umbraco.NiceUrl(yourNodeId)
    
  • Tommy Albinsson 121 posts 254 karma points
    Nov 08, 2013 @ 14:02
    Tommy Albinsson
    0

    Late post, but if you are outside the UmbracoContext? How can you figure out the url of a content node? Say if you would like to use umbraco only for editing content and having it send the content somewhere else. Then it would be ideal to be able to get the url of the content node that you get from the contentservice.

    If you use Mortens (sitereactor)example, https://github.com/sitereactor/umbraco-console-example

    how can you figure out the url of a content node when you are outside the UmbracoContext?

  • Joao Pinto 24 posts 64 karma points
    Nov 24, 2013 @ 12:43
    Joao Pinto
    0

    I'm having exactly the problem Tommy explained.

    I'm on a load balancing environment, the ADMIN is on a different machine then the WEB, so the umbracocontext is not available and can't get the URL.

    I'm looking for a way to get the node URL from contentservice.

  • Joao Pinto 24 posts 64 karma points
    Nov 24, 2013 @ 15:49
    Joao Pinto
    4

    I took a look at the Umbraco source code and I guess I found a solution, this is the code used by Umbraco backend to display the URL:

    var urlProvider = Umbraco.Web.UmbracoContext.Current.RoutingContext.UrlProvider;
    string url = urlProvider.GetUrl(IContentNode.Id);

    RoutingContext is an intenral class, so it is not usable, but I changed it to public, recompiled and now the code above works wherever I want :)

    Did some test and looks like everything is working perfectly.

  • Tommy Albinsson 121 posts 254 karma points
    Nov 26, 2013 @ 10:42
    Tommy Albinsson
    0

    Awesome, I will try this out.

  • Tom 713 posts 954 karma points
    Jan 23, 2014 @ 23:54
    Tom
    0

    I was wondeing how to do this as we use the contentservice and fluentmigrator to make changes to umbraco i.e. adding new nodes or doctypes programatically..

     

    We really need the ability to grab the url but the umbracocontext is null.

    Any thoughts?

  • Joao Pinto 24 posts 64 karma points
    Jan 24, 2014 @ 08:41
    Joao Pinto
    0

    Did you try with the solution I posted?

  • Tom 713 posts 954 karma points
    Jan 24, 2014 @ 15:14
    Tom
    0

    I wasn't too keen on recompiling umbraco source seeing as we are using the nuget packages and wanted a smoother upgrade path than previous projects..

  • Nick 14 posts 34 karma points
    Jul 15, 2014 @ 14:45
    Nick
    0

    Hi, 

    I am working on a search for my companies HR website.  I am using Examine to perform the search, (Umbraco 6.1.6).  When the site was hosted locally on my machine the search had no problems.  It has since been moved to 2 servers, which are load balanced.  Suddenly the search does not work.  The search is performed and the right results are returned.  I just cannot get each item to link to the document, (Using NiceUrl(result.Id)).  When I hover over each link it is just a '#' symbol.

    I have noticed you mentioning things around this area and was wondering if someone could take a look at my cshtml code and offer me any advice? 

    Any guidance is much appreciated, here is my source code;

    @using Examine;

    @using Umbraco.Web

    @{var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);}

    @* Get the search term from query string *@

    @{var searchTerm = Request.QueryString["search"];}

    @{var results = ExamineManager.Instance.Search(searchTerm, true); }

    @{if (results.TotalItemCount == 0)

     { 

        <p>No results found</p>

    <ul>

    <li>Check your spelling</li>

    <li>Try using single words</li>

    <li>Try searching for a less specific item</li>

    </ul>

      }

      else

      {

    <ul class="search-results">

    @foreach (var result in results)

       {

           

    <li>

    <a href="@umbracoHelper.NiceUrl(result.Id)">@result.Fields["nodeName"]</a>

    </li>    

       }

    </ul>

        }

    }

     

     

Please Sign in or register to post replies

Write your reply to:

Draft