From 7b16a119e8b000549d17adb3ab7f71cd4ecae206 Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Fri, 20 Sep 2013 17:50:36 +0700 Subject: [PATCH] Fixed the URL resolution of virtual paths when the URL is set explicitly so ~/ is now supported. --- .../Code/UpperCaseSiteMapNodeUrlResolver.cs | 5 +- .../MvcSiteMapProvider/SiteMapNode.cs | 6 +-- .../Web/UrlResolver/SiteMapNodeUrlResolver.cs | 53 +++++++++++-------- 3 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/MvcSiteMapProvider/MvcMusicStore/Code/UpperCaseSiteMapNodeUrlResolver.cs b/src/MvcSiteMapProvider/MvcMusicStore/Code/UpperCaseSiteMapNodeUrlResolver.cs index 046f38dd..6a86e33e 100644 --- a/src/MvcSiteMapProvider/MvcMusicStore/Code/UpperCaseSiteMapNodeUrlResolver.cs +++ b/src/MvcSiteMapProvider/MvcMusicStore/Code/UpperCaseSiteMapNodeUrlResolver.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Web; using MvcSiteMapProvider; +using MvcSiteMapProvider.Web; using MvcSiteMapProvider.Web.Mvc; using MvcSiteMapProvider.Web.UrlResolver; @@ -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) { } diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMapNode.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMapNode.cs index 0604a980..4034b2be 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMapNode.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/SiteMapNode.cs @@ -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 { diff --git a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/UrlResolver/SiteMapNodeUrlResolver.cs b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/UrlResolver/SiteMapNodeUrlResolver.cs index cb542f99..b862e5eb 100644 --- a/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/UrlResolver/SiteMapNodeUrlResolver.cs +++ b/src/MvcSiteMapProvider/MvcSiteMapProvider/Web/UrlResolver/SiteMapNodeUrlResolver.cs @@ -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 @@ -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 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; + } } }