Context object to be used for the URLs that IUrlHelper.RouteUrl(UrlRouteContext) generates.
コード例 #1
0
        /// <inheritdoc />
        public override string RouteUrl(UrlRouteContext routeContext)
        {
            if (routeContext == null)
            {
                throw new ArgumentNullException(nameof(routeContext));
            }

            var valuesDictionary = routeContext.Values as RouteValueDictionary ?? GetValuesDictionary(routeContext.Values);

            var endpoints = _routeValuesBasedEndpointFinder.FindEndpoints(
                new RouteValuesBasedEndpointFinderContext()
            {
                RouteName      = routeContext.RouteName,
                ExplicitValues = valuesDictionary,
                AmbientValues  = AmbientValues
            });

            var successfullyGeneratedLink = _linkGenerator.TryGetLink(
                ActionContext.HttpContext,
                endpoints,
                valuesDictionary,
                AmbientValues,
                out var link);

            if (!successfullyGeneratedLink)
            {
                return(null);
            }

            return(GenerateUrl(routeContext.Protocol, routeContext.Host, link, routeContext.Fragment));
        }
コード例 #2
0
        public void RouteUrlWithUrlRouteContext_ReturnsExpectedResult()
        {
            // Arrange
            var urlHelper = CreateUrlHelperWithDefaultRoutes();

            var routeContext = new UrlRouteContext()
            {
                RouteName = "namedroute",
                Values    = new
                {
                    Action     = "newaction",
                    Controller = "home2",
                    id         = "someid"
                },
                Fragment = "somefragment",
                Host     = "remotetown",
                Protocol = "ftp"
            };

            // Act
            var url = urlHelper.RouteUrl(routeContext);

            // Assert
            Assert.Equal("ftp://remotetown/app/named/home2/newaction/someid#somefragment", url);
        }
コード例 #3
0
        /// <inheritdoc />
        public override string RouteUrl(UrlRouteContext routeContext)
        {
            if (routeContext == null)
            {
                throw new ArgumentNullException(nameof(routeContext));
            }

            var valuesDictionary = routeContext.Values as RouteValueDictionary ?? GetValuesDictionary(routeContext.Values);

            var successfullyGeneratedLink = _linkGenerator.TryGetLink(
                new LinkGeneratorContext()
            {
                Address        = new Address(routeContext.RouteName),
                SuppliedValues = valuesDictionary,
                AmbientValues  = AmbientValues
            },
                out var link);

            if (!successfullyGeneratedLink)
            {
                return(null);
            }

            return(GenerateUrl(routeContext.Protocol, routeContext.Host, link, routeContext.Fragment));
        }
コード例 #4
0
        /// <inheritdoc />
#pragma warning disable CA1055 // URI-like return values should not be strings
        public virtual string RouteUrl(UrlRouteContext routeContext)
#pragma warning restore CA1055
        {
            if (routeContext == null)
            {
                throw new ArgumentNullException(nameof(routeContext));
            }

            routeContext.Values = AddApiVersionRouteValueIfNecessary(routeContext.Values);
            return(Url.RouteUrl(routeContext));
        }
コード例 #5
0
ファイル: UrlHelper.cs プロジェクト: wserr/AspNetCore
        /// <inheritdoc />
        public override string?RouteUrl(UrlRouteContext routeContext)
        {
            if (routeContext == null)
            {
                throw new ArgumentNullException(nameof(routeContext));
            }

            var valuesDictionary = routeContext.Values as RouteValueDictionary ?? GetValuesDictionary(routeContext.Values);
            var virtualPathData  = GetVirtualPathData(routeContext.RouteName, valuesDictionary);

            return(GenerateUrl(routeContext.Protocol, routeContext.Host, virtualPathData, routeContext.Fragment));
        }
コード例 #6
0
        public override string?RouteUrl(UrlRouteContext routeContext)
        {
            if (routeContext == null)
            {
                throw new ArgumentNullException(nameof(routeContext));
            }

            var path = _linkGenerator.GetPathByRouteValues(
                ActionContext.HttpContext,
                routeContext.RouteName,
                routeContext.Values,
                fragment: routeContext.Fragment == null ? FragmentString.Empty : new FragmentString("#" + routeContext.Fragment));

            return(GenerateUrl(routeContext.Protocol, routeContext.Host, path));
        }
コード例 #7
0
        /// <inheritdoc />
        public override string RouteUrl(UrlRouteContext routeContext)
        {
            if (routeContext == null)
            {
                throw new ArgumentNullException(nameof(routeContext));
            }

            var valuesDictionary = routeContext.Values as RouteValueDictionary ?? GetValuesDictionary(routeContext.Values);
            var pathData         = GetVirtualPathData(routeContext.RouteName, valuesDictionary);

            if (pathData is AbsolutPathData)
            {
                //we don't support changing protocol for subdomain
                return(GenerateUrl(((AbsolutPathData)pathData).Protocol, ((AbsolutPathData)pathData).Host, pathData, routeContext.Fragment));
            }
            return(GenerateUrl(routeContext.Protocol, routeContext.Host, pathData, routeContext.Fragment));
        }
コード例 #8
0
        /// <inheritdoc />
        public override string RouteUrl(UrlRouteContext routeContext)
        {
            if (routeContext == null)
            {
                throw new ArgumentNullException(nameof(routeContext));
            }

            var valuesDictionary = routeContext.Values as RouteValueDictionary ?? GetValuesDictionary(routeContext.Values);

            var successfullyGeneratedLink = _linkGenerator.TryGetLink(
                ActionContext.HttpContext,
                routeContext.RouteName,
                valuesDictionary,
                out var link);

            if (!successfullyGeneratedLink)
            {
                return(null);
            }

            return(GenerateUrl(routeContext.Protocol, routeContext.Host, link, routeContext.Fragment));
        }
コード例 #9
0
        public void Page_WithName_Works()
        {
            // Arrange
            UrlRouteContext actual    = null;
            var             routeData = new RouteData
            {
                Values =
                {
                    { "page", "ambient-page" },
                }
            };
            var actionContext = new ActionContext
            {
                RouteData = routeData,
            };
            var urlHelper = CreateMockUrlHelper(actionContext);

            urlHelper.Setup(h => h.RouteUrl(It.IsAny <UrlRouteContext>()))
            .Callback((UrlRouteContext context) => actual = context);

            // Act
            urlHelper.Object.Page("/TestPage");

            // Assert
            urlHelper.Verify();
            Assert.NotNull(actual);
            Assert.Null(actual.RouteName);
            Assert.Collection(Assert.IsType <RouteValueDictionary>(actual.Values),
                              value =>
            {
                Assert.Equal("page", value.Key);
                Assert.Equal("/TestPage", value.Value);
            });
            Assert.Null(actual.Host);
            Assert.Null(actual.Protocol);
            Assert.Null(actual.Fragment);
        }
コード例 #10
0
 /// <inheritdoc />
 public abstract string RouteUrl(UrlRouteContext routeContext);
コード例 #11
0
ファイル: UrlHelperBaseTest.cs プロジェクト: wserr/AspNetCore
 public override string RouteUrl(UrlRouteContext routeContext)
 {
     throw new NotImplementedException();
 }