Adding more overloads for IUrlHelper

(cherry picked from commit 4ebe6b5e386388914160b07c91142a9e7483134b)
This commit is contained in:
Ryan Nowak 2014-04-02 19:44:42 -07:00
parent 931d18b851
commit 37f4e2efaa
4 changed files with 113 additions and 21 deletions

View File

@ -18,7 +18,7 @@ namespace MvcSample.Web
public string Get()
{
return Url.Route(new { controller = "Home", action = "Details" });
return Url.RouteUrl(new { controller = "Home", action = "Details" }, protocol: "http", host: null, fragment: "CoolBeans!");
}
public string Link1()

View File

@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using Microsoft.AspNet.Abstractions;
using Microsoft.AspNet.DependencyInjection;
using Microsoft.AspNet.Mvc.Rendering;
@ -20,7 +22,7 @@ namespace Microsoft.AspNet.Mvc
_ambientValues = contextAccessor.Value.RouteValues;
}
public string Action(string action, string controller, object values)
public string Action(string action, string controller, object values, string protocol, string host, string fragment)
{
var valuesDictionary = values as IDictionary<string, object>;
if (valuesDictionary == null)
@ -42,22 +44,44 @@ namespace Microsoft.AspNet.Mvc
valuesDictionary["controller"] = controller;
}
return RouteCore(valuesDictionary);
var path = RouteCore(valuesDictionary);
if (path == null)
{
return null;
}
return GenerateUrl(protocol, host, path, fragment);
}
public string Route(object values)
public string RouteUrl(object values, string protocol, string host, string fragment)
{
return RouteCore(new RouteValueDictionary(values));
var path = RouteCore(new RouteValueDictionary(values));
if (path == null)
{
return null;
}
return GenerateUrl(protocol, host, path, fragment);
}
private string RouteCore(IDictionary<string, object> values)
{
var context = new VirtualPathContext(_httpContext, _ambientValues, values);
var path = _router.GetVirtualPath(context);
// We need to add the host part in here, currently blocked on http abstractions support.
// The intent is to use full URLs by default.
return _httpContext.Request.PathBase + path;
// See Routing Issue#31
PathString pathString;
if (path.Length > 0 && !path.StartsWith("/", StringComparison.Ordinal))
{
pathString = new PathString("/" + path);
}
else
{
pathString = new PathString(path);
}
return _httpContext.Request.PathBase.Add(pathString).Value;
}
public string Content([NotNull] string contentPath)
@ -75,5 +99,26 @@ namespace Microsoft.AspNet.Mvc
}
return path;
}
private string GenerateUrl(string protocol, string host, string path, string fragment)
{
Contract.Assert(path != null);
var url = path;
if (!string.IsNullOrEmpty(fragment))
{
url = url + "#" + fragment;
}
if (!string.IsNullOrEmpty(protocol) || !string.IsNullOrEmpty(host))
{
protocol = string.IsNullOrEmpty(protocol) ? "http" : protocol;
host = string.IsNullOrEmpty(host) ? _httpContext.Request.Host.Value : host;
url = protocol + "://" + host + url;
}
return url;
}
}
}

View File

@ -1,11 +1,11 @@
namespace Microsoft.AspNet.Mvc.Rendering
namespace Microsoft.AspNet.Mvc
{
public interface IUrlHelper
{
string Action(string action, string controller, object values);
string Route(object values);
string Action(string action, string controller, object values, string protocol, string host, string fragment);
string Content(string contentPath);
string RouteUrl(object values, string protocol, string host, string fragment);
}
}

View File

@ -1,25 +1,72 @@
namespace Microsoft.AspNet.Mvc.Rendering
namespace Microsoft.AspNet.Mvc
{
public static class UrlHelperExtensions
{
public static string Action([NotNull] this IUrlHelper generator)
public static string Action([NotNull] this IUrlHelper helper)
{
return generator.Action(action: null, controller: null, values: null);
return helper.Action(
action: null,
controller: null,
values: null,
protocol: null,
host: null,
fragment: null);
}
public static string Action([NotNull] this IUrlHelper generator, string action)
public static string Action([NotNull] this IUrlHelper helper, string action)
{
return generator.Action(action: action, controller: null, values: null);
return helper.Action(action, controller: null, values: null, protocol: null, host: null, fragment: null);
}
public static string Action([NotNull] this IUrlHelper generator, string action, object values)
public static string Action([NotNull] this IUrlHelper helper, string action, object values)
{
return generator.Action(action: action, controller: null, values: values);
return helper.Action(action, controller: null, values: values, protocol: null, host: null, fragment: null);
}
public static string Action([NotNull] this IUrlHelper generator, string action, string controller)
public static string Action([NotNull] this IUrlHelper helper, string action, string controller)
{
return generator.Action(action: action, controller: controller, values: null);
return helper.Action(action, controller, values: null, protocol: null, host: null, fragment: null);
}
public static string Action([NotNull] this IUrlHelper helper, string action, string controller, object values)
{
return helper.Action(action, controller, values, protocol: null, host: null, fragment: null);
}
public static string Action(
[NotNull] this IUrlHelper helper,
string action,
string controller,
object values,
string protocol)
{
return helper.Action(action, controller, values, protocol, host: null, fragment: null);
}
public static string Action(
[NotNull] this IUrlHelper helper,
string action,
string controller,
object values,
string protocol,
string host)
{
return helper.Action(action, controller, values, protocol, host, fragment: null);
}
public static string RouteUrl([NotNull] this IUrlHelper helper, object values)
{
return helper.RouteUrl(values, protocol: null, host: null, fragment: null);
}
public static string RouteUrl([NotNull] this IUrlHelper helper, object values, string protocol)
{
return helper.RouteUrl(values, protocol, host: null, fragment: null);
}
public static string RouteUrl([NotNull] this IUrlHelper helper, object values, string protocol, string host)
{
return helper.RouteUrl(values, protocol, host, fragment: null);
}
}
}