Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Commit

Permalink
[Fixes #2896] Made UrlHelper.Content behavior consistent with MVC 5
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaybhargavb committed Aug 7, 2015
1 parent 94fad91 commit b5237b2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,19 @@ protected virtual string GeneratePathFromRoute(string routeName, IDictionary<str
/// <inheritdoc />
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;
}

/// <inheritdoc />
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit b5237b2

Please sign in to comment.