Skip to content

Commit

Permalink
Fixed the URL resolution of virtual paths when the URL is set explici…
Browse files Browse the repository at this point in the history
…tly so ~/ is now supported.
  • Loading branch information
NightOwl888 committed Sep 21, 2013
1 parent 59b3230 commit 7b16a11
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Web;
using MvcSiteMapProvider;
using MvcSiteMapProvider.Web;
using MvcSiteMapProvider.Web.Mvc;
using MvcSiteMapProvider.Web.UrlResolver;

Expand All @@ -14,8 +15,8 @@ namespace MvcMusicStore.Code
public class UpperCaseSiteMapNodeUrlResolver
: SiteMapNodeUrlResolver
{
public UpperCaseSiteMapNodeUrlResolver(IMvcContextFactory mvcContextFactory)
: base(mvcContextFactory)
public UpperCaseSiteMapNodeUrlResolver(IMvcContextFactory mvcContextFactory, IUrlPath urlPath)
: base(mvcContextFactory, urlPath)
{
}

Expand Down
6 changes: 3 additions & 3 deletions src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMapNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,11 @@ public override string Url
return this.ResolvedUrl;
}
// Only resolve the url if an absolute url is not already set
if (String.IsNullOrEmpty(this.UnresolvedUrl) || !this.HasAbsoluteUrl())
if (this.HasAbsoluteUrl())
{
return GetResolvedUrl();
return this.UnresolvedUrl;
}
return this.UnresolvedUrl;
return GetResolvedUrl();
}
set
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ public class SiteMapNodeUrlResolver
: SiteMapNodeUrlResolverBase
{
public SiteMapNodeUrlResolver(
IMvcContextFactory mvcContextFactory
IMvcContextFactory mvcContextFactory,
IUrlPath urlPath
)
{
if (mvcContextFactory == null)
throw new ArgumentNullException("mvcContextFactory");
if (urlPath == null)
throw new ArgumentNullException("urlPath");

this.mvcContextFactory = mvcContextFactory;
this.urlPath = urlPath;
}

protected readonly IMvcContextFactory mvcContextFactory;
protected readonly IUrlPath urlPath;

#region ISiteMapNodeUrlResolver Members

Expand All @@ -38,41 +44,46 @@ public override string ResolveUrl(ISiteMapNode node, string area, string control
{
if (!String.IsNullOrEmpty(node.UnresolvedUrl))
{
if (node.UnresolvedUrl.StartsWith("~"))
{
return VirtualPathUtility.ToAbsolute(node.UnresolvedUrl);
}
else
{
return node.UnresolvedUrl;
}
return this.ResolveVirtualPath(node);
}
return this.ResolveRouteUrl(node, area, controller, action, routeValues);
}

var urlHelper = mvcContextFactory.CreateUrlHelper();
#endregion

protected virtual string ResolveVirtualPath(ISiteMapNode node)
{
var url = node.UnresolvedUrl;
if (!urlPath.IsAbsoluteUrl(url))
{
return urlPath.MakeVirtualPathAppAbsolute(urlPath.Combine(urlPath.AppDomainAppVirtualPath, url));
}
return url;
}

string returnValue;
protected virtual string ResolveRouteUrl(ISiteMapNode node, string area, string controller, string action, IDictionary<string, object> routeValues)
{
string result = String.Empty;
var urlHelper = mvcContextFactory.CreateUrlHelper();
var routeValueDictionary = new RouteValueDictionary(routeValues);

if (!string.IsNullOrEmpty(node.Route))
{
routeValueDictionary.Remove("route");
returnValue = urlHelper.RouteUrl(node.Route, routeValueDictionary);
result = urlHelper.RouteUrl(node.Route, routeValueDictionary);
}
else
{
returnValue = urlHelper.Action(action, controller, routeValueDictionary);
result = urlHelper.Action(action, controller, routeValueDictionary);
}

if (string.IsNullOrEmpty(returnValue))
if (string.IsNullOrEmpty(result))
{
// fixes #115 - UrlResolver should not throw exception.
return VirtualPathUtility.ToAbsolute("~/") + Guid.NewGuid().ToString();
return urlPath.MakeVirtualPathAppAbsolute("~") + Guid.NewGuid().ToString();
}
else
{
return returnValue;
}
}

#endregion
return result;
}
}
}

0 comments on commit 7b16a11

Please sign in to comment.