External Url problem

Feb 25, 2011 at 4:55 PM

Hi,

I'm trying to include an external Url in the sitemap using the url attribute like this :

 

<mvcSiteMapNode title="External Blog" url="http://www.myblog.com" />

This fails giving a System.Web.HttpException: 'http://www.myblog.com' is not a valid virtual path. Is there another way to implement this?


Thanx,

Kostas

 

Mar 1, 2011 at 3:48 PM

This is a restriction in .Net's SiteMapProvider, there's probably a few different ways round it but this is the one I'm using, it's a little bit hacky but at the same time pretty simple and doesn't require digging into complex parts of the SiteMap code.

Basically replace the : with %3A (you could use anything you wanted here but using a UrlEscaped value seems right to me). Create an extension method for SiteMapNodeModel called SafeUrl:

 

public static string SafeUrl(this SiteMapNodeModel node)
{
	if (node.Url != null && node.Url.StartsWith("http%3A"))
	{
		return node.Url.Replace("%3A", ":");
	}
	return node.Url;
}

 

 

Then either create a new custom DisplayTemplate for SiteMapNodeModel or amend the one in the project and replace

<a href="<%=Model.Url %>"><%=Model.Title %></a>

with

<a href="<%=Model.SafeUrl() %>"><%=Model.Title %></a>.

Ideally you'd also make SafeUrl only replace the first instance of %3A if you think you'd have any external urls containing it.

Mar 21, 2011 at 8:46 PM

Thank you chaoretasty.  I'm sure this is an excellent workaround for the external URL problem.  Now I need to learn how to implement an extension method for the MvcSiteMapProvider and how to create a new custom DisplayTemplate.

But, I don't understand that this is a restriction in .Net's SiteMapProvider.  I implemented version 1.0 of this provider and had no problem referencing an external URL.  I think a menu system should allow external URL's, so the site map provider should handle this.

Maarten, is this possible?

Mar 23, 2011 at 10:03 AM

Hi,

Even though I've already implemented the workaround suggested (or something similar), I totally agree with tebo that this should be something supported out of the box. Could we somehow give some importance to this issue so that we can see it implemented in a next version of the MVCSitemap?

Thank, Kostas

Coordinator
Mar 25, 2011 at 9:56 PM

I have lookad at the 1.0 sources and cant find a fix for it. Any suggestions?

Mar 30, 2011 at 1:20 PM

The issue I believe is with the StaticSiteMapProvider's AddNode function which requires virtual paths. I believe back in version 1 you could mix node types in the sitemap (it was a long time ago I used it) and I think using the normal SiteMapNode element let you give an external URL.

Another work around and a bit more info can be found http://frozengraphics.wordpress.com/2009/11/28/custom-sitemapprovider-and-external-links/ It looks like a nice little work around and possibly something that could be built into MVCSiteMapProvider,

Coordinator
Apr 5, 2011 at 6:44 AM

Implemented for 3.1.0. Thanks!