+1-888-365-2779
Try Now
More in this section

Forums / General Discussions / Getting a full URL from a SiteMapNode

Getting a full URL from a SiteMapNode

4 posts, 1 answered
  1. Kurren
    Kurren avatar
    23 posts
    Registered:
    08 Jun 2013
    03 Sep 2013
    Link to this post
    Hi

    I'm making my own MVC navigation bar widget. I'm just beginning, so far it's pretty simple. All I want to do is list the links to each page that should go in the navigation bar.

    Controller:
    public class NavigationBarController : Controller
    {
        public ActionResult Index()
        {
     
            var provider = SiteMapBase.GetSiteMapProvider("FrontendSiteMap");
     
            var topNavNodes = provider.GetChildNodes(provider.RootNode);
     
            return View("NavigationBar", topNavNodes);
        }
     
    }

    View:
    @model SiteMapNodeCollection
     
    <ul>
        @foreach (var baseNode in Model)
        {
            <li>
                <a href ="@baseNode.Url">@baseNode.Title</a>
            </li>
        }
    </ul>

    The problem is the links are all broken, they turn out like: 
    http://localhost:5291/MyWebSite/~/products

    How can I get the absolute URL (for SEO purposes too) from a SiteMapNode? Or am I going about this the wrong way?

    Thanks for your help,
    Kurren
    Answered
  2. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    03 Sep 2013 in reply to Kurren
    Link to this post
    Hi Kurren,

    You can use the Url.Content method that will take a relative Url and returns an absolute Url.
    <ul>
        @foreach (var baseNode in Model)
        {
            <li>
                <a href ="@Url.Content(baseNode.Ur)">@baseNode.Title</a>
            </li>
        }
    </ul>

    On the server-side you could use something like:
    VirtualPathProvider.ToAbsolute()

    Kind regards,
    Daniel

  3. Kurren
    Kurren avatar
    23 posts
    Registered:
    08 Jun 2013
    04 Sep 2013 in reply to Daniel Plomp
    Link to this post
    Hi Daniel

    Thanks for your reply. It worked like a charm.

    Kurren
  4. Daniel Plomp
    Daniel Plomp avatar
    952 posts
    Registered:
    18 Feb 2004
    04 Sep 2013 in reply to Kurren
    Link to this post
    Hi Kurren,

    That is great to hear.
    Glad I could help.

    Kind regards,
    Daniel
4 posts, 1 answered