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

Reacting to Routing changes #3861

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions src/Microsoft.AspNet.Mvc.Core/Routing/UrlHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Text;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Routing;

Expand All @@ -14,7 +15,6 @@ namespace Microsoft.AspNet.Mvc.Routing
/// </summary>
public class UrlHelper : IUrlHelper
{

/// <summary>
/// Initializes a new instance of the <see cref="UrlHelper"/> class using the specified action context and
/// action selector.
Expand All @@ -28,7 +28,7 @@ public UrlHelper(ActionContext actionContext)
{
throw new ArgumentNullException(nameof(actionContext));
}

ActionContext = actionContext;
}

Expand Down Expand Up @@ -79,7 +79,7 @@ public virtual string Action(UrlActionContext actionContext)
valuesDictionary["controller"] = actionContext.Controller;
}

var path = GeneratePathFromRoute(valuesDictionary);
var path = GeneratePathFromRoute(routeName: null, values: valuesDictionary);
if (path == null)
{
return null;
Expand Down Expand Up @@ -120,11 +120,6 @@ public virtual string RouteUrl(UrlRouteContext routeContext)
return GenerateUrl(routeContext.Protocol, routeContext.Host, path, routeContext.Fragment);
}

private string GeneratePathFromRoute(RouteValueDictionary values)
{
return GeneratePathFromRoute(routeName: null, values: values);
}

/// <summary>
/// Generates the absolute path of the url for the specified route values by
/// using the specified route name.
Expand All @@ -143,15 +138,48 @@ protected virtual string GeneratePathFromRoute(string routeName, RouteValueDicti

// VirtualPathData.VirtualPath returns string.Empty for null.
Debug.Assert(pathData.VirtualPath != null);

var fullPath = HttpContext.Request.PathBase.Add(pathData.VirtualPath).Value;
if (fullPath.Length == 0)
var pathBase = HttpContext.Request.PathBase;
if (!pathBase.HasValue)
{
return "/";
if (pathData.VirtualPath.Length == 0)
{
return "/";
}
else if (!pathData.VirtualPath.StartsWith("/", StringComparison.Ordinal))
{
return "/" + pathData.VirtualPath;
}
else
{
return pathData.VirtualPath;
}
}
else
{
return fullPath;
if (pathData.VirtualPath.Length == 0)
{
return pathBase;
}
else
{
var builder = new StringBuilder(
pathBase.Value,
pathBase.Value.Length + pathData.VirtualPath.Length);

if (pathBase.Value.EndsWith("/", StringComparison.Ordinal))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
builder.Length--;
}

if (!pathData.VirtualPath.StartsWith("/", StringComparison.Ordinal))
{
builder.Append("/");
}

builder.Append(pathData.VirtualPath);

return builder.ToString();
}
}
}

Expand Down Expand Up @@ -187,9 +215,7 @@ public virtual string Link(string routeName, object values)

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
Debug.Assert(path != null);

var url = path;
if (!string.IsNullOrEmpty(fragment))
{
Expand Down
Loading