From 3d30fd653ec2e9d44296a4c62a274e578da145fe Mon Sep 17 00:00:00 2001 From: Ajay Bhargav Baaskaran Date: Thu, 12 Feb 2015 17:04:24 -0800 Subject: [PATCH] added Link method to IUrlHelper --- .../ActionResults/CreatedAtRouteResult.cs | 3 +- src/Microsoft.AspNet.Mvc.Core/IUrlHelper.cs | 11 +++ src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs | 12 +++ .../CreatedAtRouteResultTests.cs | 2 +- .../RemoteAttributeTest.cs | 5 ++ .../UrlHelperTest.cs | 89 +++++++++++++++++++ 6 files changed, 119 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNet.Mvc.Core/ActionResults/CreatedAtRouteResult.cs b/src/Microsoft.AspNet.Mvc.Core/ActionResults/CreatedAtRouteResult.cs index 87491c4bec..10667350f8 100644 --- a/src/Microsoft.AspNet.Mvc.Core/ActionResults/CreatedAtRouteResult.cs +++ b/src/Microsoft.AspNet.Mvc.Core/ActionResults/CreatedAtRouteResult.cs @@ -60,10 +60,9 @@ namespace Microsoft.AspNet.Mvc /// protected override void OnFormatting([NotNull] ActionContext context) { - var request = context.HttpContext.Request; var urlHelper = UrlHelper ?? context.HttpContext.RequestServices.GetRequiredService(); - var url = urlHelper.RouteUrl(RouteName, RouteValues, request.Scheme, request.Host.ToUriComponent()); + var url = urlHelper.Link(RouteName, RouteValues); if (string.IsNullOrEmpty(url)) { diff --git a/src/Microsoft.AspNet.Mvc.Core/IUrlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/IUrlHelper.cs index 81f2251bff..867fcd189c 100644 --- a/src/Microsoft.AspNet.Mvc.Core/IUrlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/IUrlHelper.cs @@ -55,5 +55,16 @@ namespace Microsoft.AspNet.Mvc /// The context object for the generated URLs for a route. /// The fully qualified or absolute URL. string RouteUrl([NotNull] UrlRouteContext routeContext); + + /// + /// Generates an absolute URL using the specified route name and values. + /// + /// The name of the route that is used to generate the URL. + /// An object that contains the route values. + /// The generated absolute URL. + /// + /// The protocol and host is obtained from the current request. + /// + string Link(string routeName, object values); } } diff --git a/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs b/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs index 607c8d7cba..7d194276a9 100644 --- a/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs +++ b/src/Microsoft.AspNet.Mvc.Core/UrlHelper.cs @@ -127,6 +127,18 @@ namespace Microsoft.AspNet.Mvc return GenerateClientUrl(_httpContext.Request.PathBase, contentPath); } + /// + public virtual string Link(string routeName, object values) + { + return RouteUrl(new UrlRouteContext() + { + RouteName = routeName, + Values = values, + Protocol = _httpContext.Request.Scheme, + Host = _httpContext.Request.Host.ToUriComponent() + }); + } + private static string GenerateClientUrl([NotNull] PathString applicationPath, [NotNull] string path) { diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtRouteResultTests.cs b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtRouteResultTests.cs index 320bfc0503..c6a258bc6a 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtRouteResultTests.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/ActionResults/CreatedAtRouteResultTests.cs @@ -115,7 +115,7 @@ namespace Microsoft.AspNet.Mvc private static IUrlHelper GetMockUrlHelper(string returnValue) { var urlHelper = new Mock(); - urlHelper.Setup(o => o.RouteUrl(It.IsAny())).Returns(returnValue); + urlHelper.Setup(o => o.Link(It.IsAny(), It.IsAny())).Returns(returnValue); return urlHelper.Object; } diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/RemoteAttributeTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/RemoteAttributeTest.cs index 9fb596b2f4..f3e58e8874 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/RemoteAttributeTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/RemoteAttributeTest.cs @@ -613,6 +613,11 @@ namespace Microsoft.AspNet.Mvc throw new NotImplementedException(); } + public string Link(string routeName, object values) + { + throw new NotImplementedException(); + } + public string RouteUrl(UrlRouteContext routeContext) { Assert.Equal(_routeName, routeContext.RouteName); diff --git a/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs b/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs index 24bbbccfb6..791ad5b921 100644 --- a/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs +++ b/test/Microsoft.AspNet.Mvc.Core.Test/UrlHelperTest.cs @@ -728,6 +728,82 @@ namespace Microsoft.AspNet.Mvc Assert.Equal("https://remotelyhost/app/home3/contact#somefragment", url); } + [Fact] + public void LinkWithAllParameters_ReturnsExpectedResult() + { + // Arrange + var services = GetServices(); + var urlHelper = CreateUrlHelperWithRouteCollection(services, "/app"); + + // Act + var url = urlHelper.Link("namedroute", + new + { + Action = "newaction", + Controller = "home", + id = "someid" + }); + + // Assert + Assert.Equal("http://localhost/app/named/home/newaction/someid", url); + } + + [Fact] + public void LinkWithNullRouteName_ReturnsExpectedResult() + { + // Arrange + var services = GetServices(); + var urlHelper = CreateUrlHelperWithRouteCollection(services, "/app"); + + // Act + var url = urlHelper.Link(null, + new + { + Action = "newaction", + Controller = "home", + id = "someid" + }); + + // Assert + Assert.Equal("http://localhost/app/home/newaction/someid", url); + } + + [Fact] + public void LinkWithDefaultsAndNullRouteValues_ReturnsExpectedResult() + { + // Arrange + var services = GetServices(); + var routeCollection = GetRouter(services, "MyRouteName", "any/url"); + var urlHelper = CreateUrlHelper("/app", routeCollection); + + // Act + var url = urlHelper.Link("MyRouteName", null); + + // Assert + Assert.Equal("http://localhost/app/any/url", url); + } + + [Fact] + public void LinkWithCustomHostAndProtocol_ReturnsExpectedResult() + { + // Arrange + var services = GetServices(); + var routeCollection = GetRouter(services, "MyRouteName", "any/url"); + var urlHelper = CreateUrlHelper("myhost", "https", routeCollection); + + // Act + var url = urlHelper.Link("namedroute", + new + { + Action = "newaction", + Controller = "home", + id = "someid" + }); + + // Assert + Assert.Equal("https://myhost/named/home/newaction/someid", url); + } + private static HttpContext CreateHttpContext( IServiceProvider services, string appRoot) @@ -782,6 +858,19 @@ namespace Microsoft.AspNet.Mvc return new UrlHelper(actionContext, actionSelector.Object); } + private static UrlHelper CreateUrlHelper(string host, string protocol, IRouter router) + { + var services = GetServices(); + var context = CreateHttpContext(services, string.Empty); + context.Request.Host = new HostString(host); + context.Request.Scheme = protocol; + + var actionContext = CreateActionContext(context, router); + + var actionSelector = new Mock(MockBehavior.Strict); + return new UrlHelper(actionContext, actionSelector.Object); + } + private static UrlHelper CreateUrlHelper(IScopedInstance contextAccessor) { var actionSelector = new Mock(MockBehavior.Strict);