From 37f4e2efaaa478214bb24dda830cd8dbc2c89294 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 2 Apr 2014 19:44:42 -0700 Subject: [PATCH] Adding more overloads for IUrlHelper (cherry picked from commit 4ebe6b5e386388914160b07c91142a9e7483134b) --- samples/MvcSample.Web/LinkController.cs | 2 +- src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs | 59 +++++++++++++++-- .../IUrlHelper.cs | 8 +-- .../UrlHelperExtensions.cs | 65 ++++++++++++++++--- 4 files changed, 113 insertions(+), 21 deletions(-) diff --git a/samples/MvcSample.Web/LinkController.cs b/samples/MvcSample.Web/LinkController.cs index 2dc7b843f7..6190b3263c 100644 --- a/samples/MvcSample.Web/LinkController.cs +++ b/samples/MvcSample.Web/LinkController.cs @@ -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() diff --git a/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs index f42183b3bf..e7fce99984 100644 --- a/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs @@ -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; 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 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; + } } } diff --git a/src/Microsoft.AspNet.Mvc.Rendering/IUrlHelper.cs b/src/Microsoft.AspNet.Mvc.Rendering/IUrlHelper.cs index bba40b8583..d8505dfd5b 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/IUrlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/IUrlHelper.cs @@ -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); } } diff --git a/src/Microsoft.AspNet.Mvc.Rendering/UrlHelperExtensions.cs b/src/Microsoft.AspNet.Mvc.Rendering/UrlHelperExtensions.cs index eeb795f829..fbbda27443 100644 --- a/src/Microsoft.AspNet.Mvc.Rendering/UrlHelperExtensions.cs +++ b/src/Microsoft.AspNet.Mvc.Rendering/UrlHelperExtensions.cs @@ -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); } } }