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

Commit

Permalink
Reacting to Routing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavkm committed Jan 4, 2016
1 parent 568b061 commit 78be331
Show file tree
Hide file tree
Showing 2 changed files with 245 additions and 146 deletions.
66 changes: 52 additions & 14 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 @@ -28,7 +29,7 @@ public UrlHelper(ActionContext actionContext)
{
throw new ArgumentNullException(nameof(actionContext));
}

ActionContext = actionContext;
}

Expand Down Expand Up @@ -78,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 @@ -119,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 @@ -142,15 +138,59 @@ 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
{
return !pathData.VirtualPath.StartsWith("/", StringComparison.Ordinal)
? "/" + pathData.VirtualPath
: pathData.VirtualPath;
}
}
else
{
return fullPath;
if (pathData.VirtualPath.Length == 0)
{
return pathBase;
}
else
{
var requiresLeadingSlash = !pathData.VirtualPath.StartsWith("/", StringComparison.Ordinal);
var virtualPathStartIndex = 0;
if (pathBase.Value.EndsWith("/", StringComparison.Ordinal))
{
if (!requiresLeadingSlash)
{
// If the base path has a trailing slash and the virtual path string has a leading slash,
// we need to trim one of them.
virtualPathStartIndex = 1;

}

requiresLeadingSlash = false;
}

var virtualPathLength = pathData.VirtualPath.Length - virtualPathStartIndex;

var builder = new StringBuilder(
pathBase.Value.Length
+ (requiresLeadingSlash ? 1 : 0)
+ virtualPathLength);

builder.Append(pathBase.Value);
if (requiresLeadingSlash)
{
builder.Append("/");
}
builder.Append(pathData.VirtualPath, virtualPathStartIndex, virtualPathLength);

return builder.ToString();
}
}
}

Expand Down Expand Up @@ -186,9 +226,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

0 comments on commit 78be331

Please sign in to comment.