/// <inheritdoc/>
        public override string GetPathByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
        {
            object odataPathValue;

            if (values.TryGetValue("odataPath", out odataPathValue))
            {
                string odataPath = odataPathValue as string;
                if (odataPath != null)
                {
                    IPerRouteContainer perRouteContainer = httpContext.RequestServices.GetRequiredService <IPerRouteContainer>();
                    string             routePrefix       = perRouteContainer.GetRoutePrefix(httpContext.Request.ODataFeature().RouteName);

                    bool canGenerateDirectLink = routePrefix == null || routePrefix.IndexOf('{') == -1;
                    if (!canGenerateDirectLink)
                    {
                        routePrefix = BindPrefixTemplate(routePrefix, values, ambientValues);
                    }
                    string link = CombinePathSegments(routePrefix, odataPath);
                    link = UriEncode(link);
                    return(link);
                }
            }

            return(_innerGenerator.GetPathByAddress(httpContext, address, values, ambientValues, pathBase, fragment, options));
        }
Ejemplo n.º 2
0
        /// <inheritdoc/>
        public override string GetPathByAddress <TAddress>(HttpContext httpContext, TAddress address, RouteValueDictionary values, RouteValueDictionary ambientValues = null, PathString?pathBase = null, FragmentString fragment = default, LinkOptions options = null)
        {
            object odataPathValue;

            if (values.TryGetValue("odataPath", out odataPathValue))
            {
                string odataPath = odataPathValue as string;
                if (odataPath != null)
                {
                    IPerRouteContainer perRouteContainer = httpContext.RequestServices.GetRequiredService <IPerRouteContainer>();
                    string             routePrefix       = perRouteContainer.GetRoutePrefix(httpContext.Request.ODataFeature().RouteName);

                    bool canGenerateDirectLink = routePrefix == null || routePrefix.IndexOf('{') == -1;
                    if (!canGenerateDirectLink)
                    {
                        routePrefix = BindPrefixTemplate(routePrefix, values, ambientValues);
                    }
                    string link = CombinePathSegments(routePrefix, odataPath);
                    link = UriEncode(link);

                    // A workaround to include the PathBase, a good solution is to use ASP.NET Core provided the APIs
                    // at https://github.com/dotnet/aspnetcore/blob/master/src/Http/Http.Extensions/src/UriHelper.cs#L48
                    // to build the absolute Uri. But, here only needs the "PathBase + Path (without OData path)",
                    HttpRequest request = httpContext.Request;
                    if (request != null && request.PathBase != null && request.PathBase.HasValue)
                    {
                        return(request.PathBase.Value + "/" + link);
                    }

                    return(link);
                }
            }

            return(_innerGenerator.GetPathByAddress(httpContext, address, values, ambientValues, pathBase, fragment, options));
        }