From d2386d7ded714dd1d16a8b5e84ff4df3897b88b5 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Fri, 4 Apr 2014 12:26:55 -0700 Subject: [PATCH] CR Feedback --- samples/MvcSample.Web/LinkController.cs | 1 + src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs | 35 +++++++++++++--------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/samples/MvcSample.Web/LinkController.cs b/samples/MvcSample.Web/LinkController.cs index 6190b3263c..a8c1227086 100644 --- a/samples/MvcSample.Web/LinkController.cs +++ b/samples/MvcSample.Web/LinkController.cs @@ -18,6 +18,7 @@ namespace MvcSample.Web public string Get() { + // Creates a url like: http://localhost:58195/Home/Details#CoolBeans! return Url.RouteUrl(new { controller = "Home", action = "Details" }, protocol: "http", host: null, fragment: "CoolBeans!"); } diff --git a/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs index e7fce99984..b6fd39457b 100644 --- a/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs @@ -44,7 +44,7 @@ namespace Microsoft.AspNet.Mvc valuesDictionary["controller"] = controller; } - var path = RouteCore(valuesDictionary); + var path = GeneratePathFromRoute(valuesDictionary); if (path == null) { return null; @@ -55,7 +55,7 @@ namespace Microsoft.AspNet.Mvc public string RouteUrl(object values, string protocol, string host, string fragment) { - var path = RouteCore(new RouteValueDictionary(values)); + var path = GeneratePathFromRoute(new RouteValueDictionary(values)); if (path == null) { return null; @@ -64,24 +64,19 @@ namespace Microsoft.AspNet.Mvc return GenerateUrl(protocol, host, path, fragment); } - private string RouteCore(IDictionary values) + private string GeneratePathFromRoute(IDictionary values) { var context = new VirtualPathContext(_httpContext, _ambientValues, values); var path = _router.GetVirtualPath(context); // See Routing Issue#31 - PathString pathString; if (path.Length > 0 && !path.StartsWith("/", StringComparison.Ordinal)) { - pathString = new PathString("/" + path); - } - else - { - pathString = new PathString(path); + path = "/" + path; } - return _httpContext.Request.PathBase.Add(pathString).Value; + return _httpContext.Request.PathBase.Add(new PathString(path)).Value; } public string Content([NotNull] string contentPath) @@ -102,23 +97,35 @@ namespace Microsoft.AspNet.Mvc private string GenerateUrl(string protocol, string host, string path, string fragment) { + // We should have a robust and centrallized version of this code. See HttpAbstractions#28 + Contract.Assert(path != null); var url = path; if (!string.IsNullOrEmpty(fragment)) { - url = url + "#" + fragment; + url += "#" + fragment; } - if (!string.IsNullOrEmpty(protocol) || !string.IsNullOrEmpty(host)) + if (string.IsNullOrEmpty(protocol) && string.IsNullOrEmpty(host)) + { + // We're returning a partial url (just path + query + fragment), but we still want it + // to be rooted. + if (!url.StartsWith("/", StringComparison.Ordinal)) + { + url = "/" + url; + } + + return url; + } + else { protocol = string.IsNullOrEmpty(protocol) ? "http" : protocol; host = string.IsNullOrEmpty(host) ? _httpContext.Request.Host.Value : host; url = protocol + "://" + host + url; + return url; } - - return url; } } }