Getting a full URL from a SiteMapNode

Posted by Community Admin on 03-Aug-2018 16:40

Getting a full URL from a SiteMapNode

All Replies

Posted by Community Admin on 03-Sep-2013 00:00

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: 
localhost:5291/.../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

Posted by Community Admin on 03-Sep-2013 00:00

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

Posted by Community Admin on 04-Sep-2013 00:00

Hi Daniel

Thanks for your reply. It worked like a charm.

Kurren

Posted by Community Admin on 04-Sep-2013 00:00

Hi Kurren,

That is great to hear.
Glad I could help.

Kind regards,
Daniel

This thread is closed