Example #1
0
        public static bool ParseSingleEntityLink(this ODataController oDataController, Uri link, out IEdmEntitySet entitySet, out object key)
        {
            Contract.Requires <ArgumentNullException>(oDataController != null);
            Contract.Requires <ArgumentNullException>(link != null);
            HttpRequestMessage request = oDataController.Request;

            // Get the route that was used for this request.
            IHttpRoute route = request.GetRouteData().Route;

            // Create an equivalent self-hosted route.
            IHttpRoute newRoute = new HttpRoute(route.RouteTemplate,
                                                new HttpRouteValueDictionary(route.Defaults),
                                                new HttpRouteValueDictionary(route.Constraints),
                                                new HttpRouteValueDictionary(route.DataTokens), route.Handler);

            // Create a fake GET request for the link URI.
            var tmpRequest = new HttpRequestMessage(HttpMethod.Get, link);

            // Send this request through the routing process.
            var routeData = newRoute.GetRouteData(request.GetRequestContext().VirtualPathRoot, tmpRequest);

            // If the GET request matches the route, use the path segments to find the key.
            if (routeData != null)
            {
                ODataPath path = tmpRequest.ODataProperties().Path;
                if (path.PathTemplate == "~/entityset/key")
                {
                    entitySet = path.EntitySet;
                    var keySegment = path.Segments.OfType <KeyValuePathSegment>().FirstOrDefault();
                    if (keySegment != null)
                    {
                        // Convert the segment into the key type.
                        key = ODataUriUtils.ConvertFromUriLiteral(keySegment.Value, ODataVersion.V3, request.ODataProperties().Model, entitySet.GetSingleKeyType());
                        return(true);
                    }
                }
            }

            entitySet = null;
            key       = null;
            return(false);
        }