示例#1
0
        public Uri Provide(LinkRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var routeName = request.GetRouteName();

            if (string.IsNullOrWhiteSpace(routeName))
            {
                throw new ArgumentException($"Parameter '{nameof(request)}' must contain a '{LinkRequestBuilder.RouteNameKey}' value.");
            }

            var routeValues = request.GetRouteValues();
            var url         = routeValues == null?
                              urlHelper.Value.RouteUrl(routeName) :
                                  urlHelper.Value.RouteUrl(routeName, routeValues);

            url = FormatUrlPath(url);
            if (string.IsNullOrWhiteSpace(url) || !Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
            {
                throw new InvalidOperationException($"The given '{LinkRequestBuilder.RouteNameKey}' to retrieve the URL did not provide a valid value. Value of '{LinkRequestBuilder.RouteNameKey}': {routeName}");
            }

            return(new Uri(url, UriKind.RelativeOrAbsolute));
        }
        public Uri Provide(LinkRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            var routeName = request.GetRouteName();

            if (string.IsNullOrWhiteSpace(routeName))
            {
                throw new ArgumentException($"Parameter '{nameof(request)}' must contain a '{LinkRequestBuilder.RouteNameKey}' value.");
            }

            if (request.IsTemplated())
            {
                var template = GetTemplate(routeName);
                if (string.IsNullOrWhiteSpace(template))
                {
                    throw new InvalidOperationException($"The given '{LinkRequestBuilder.RouteNameKey}' to retrieve the URL template returned a null or empty value. Value of '{LinkRequestBuilder.RouteNameKey}': {routeName}");
                }

                return(new Uri($"/{template}", UriKind.Relative));
            }

            return(nonTemplatedUrlPathProvider.Provide(request));
        }
        public void GetRouteNameShouldReturnRouteNameWhenRouteNameExists()
        {
            LinkRequest request = LinkRequestBuilder.CreateWithRouteName(TestRouteName);

            var result = request.GetRouteName();

            result.Should().Be(TestRouteName);
        }
        public void GetRouteNameShouldReturnNullWhenRouteNameDoesNotExist()
        {
            // Valid use of LinkRequest ctor to simulate a request w/o a routeName value.
            var request = new LinkRequest(new Dictionary <string, object>
            {
                { "Filler", "filler" }
            });

            var result = request.GetRouteName();

            result.Should().BeNull();
        }
示例#5
0
        public void Enrich(LinkRequest request, LinkDataWriter writer)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (writer == null)
            {
                throw new ArgumentNullException(nameof(writer));
            }

            var httpMethod = GetHttpMethod(request.GetRouteName());

            if (!string.IsNullOrWhiteSpace(httpMethod))
            {
                writer.Write(HttpMethodKey, httpMethod);
            }
        }
        public Link GetLink(LinkRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            if (!request.ContainsKey(LinkRequestBuilder.RouteNameKey))
            {
                throw new ArgumentException($"Parameter '{nameof(request)}' must contain a '{LinkRequestBuilder.RouteNameKey}' value.");
            }

            if (string.IsNullOrWhiteSpace(request.GetRouteName()))
            {
                throw new ArgumentException($"The '{LinkRequestBuilder.RouteNameKey}' item in the '{nameof(request)}' parameter must not be null or empty.");
            }

            return(DoGetLink(request));
        }