From b5237b29b55e3045e2e3c3f9f10cf72861e3f206 Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Fri, 7 Aug 2015 15:17:19 -0700 Subject: [PATCH] [Fixes #2896] Made UrlHelper.Content behavior consistent with MVC 5 --- src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs | 22 ++++++++----------- .../UrlHelperTest.cs | 1 + 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs index 186cceaf7a..97ed3e0096 100644 --- a/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs @@ -122,12 +122,19 @@ protected virtual string GeneratePathFromRoute(string routeName, IDictionary public virtual string Content(string contentPath) { - if (contentPath == null) + if (string.IsNullOrEmpty(contentPath)) { return null; } + else if (contentPath[0] == '~') + { + var segment = new PathString(contentPath.Substring(1)); + var applicationPath = _httpContext.Request.PathBase; - return GenerateClientUrl(_httpContext.Request.PathBase, contentPath); + return applicationPath.Add(segment).Value; + } + + return contentPath; } /// @@ -142,17 +149,6 @@ public virtual string Link(string routeName, object values) }); } - private static string GenerateClientUrl([NotNull] PathString applicationPath, - [NotNull] string path) - { - if (path.StartsWith("~/", StringComparison.Ordinal)) - { - var segment = new PathString(path.Substring(1)); - return applicationPath.Add(segment).Value; - } - return path; - } - 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 diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs index 0abc6e6216..9794c8e971 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs @@ -45,6 +45,7 @@ public void Content_ReturnsContentPath_WhenItDoesNotStartWithToken(string appRoo [InlineData("/", "~/", "/")] [InlineData("/myapproot", "~/", "/myapproot/")] [InlineData("", "~/Home/About", "/Home/About")] + [InlineData("/", "~", "/")] [InlineData("/myapproot", "~/Content/bootstrap.css", "/myapproot/Content/bootstrap.css")] public void Content_ReturnsAppRelativePath_WhenItStartsWithToken(string appRoot, string contentPath,