コード例 #1
0
        public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup <string, HttpActionDescriptor> actionMap)
        {
            string requestMethod               = controllerContext.Request.Method.ToString().ToUpperInvariant();
            TestControllerContext context      = new TestControllerContext(controllerContext);
            IList <string>        actionLookup = actionMap.Select(g => g.Key).Distinct().ToList();

            return(SelectAction(requestMethod, odataPath, context, actionLookup));
        }
コード例 #2
0
        public override string SelectAction(RouteContext routeContext, SelectControllerResult controllerResult, IEnumerable <ControllerActionDescriptor> actionDescriptors)
        {
            string                requestMethod = routeContext.HttpContext.Request.Method;
            ODataPath             odataPath     = routeContext.HttpContext.ODataFeature().Path;
            TestControllerContext context       = new TestControllerContext(routeContext);
            IList <string>        actionLookup  = actionDescriptors.Select(a => a.ActionName).Distinct().ToList();

            return(SelectAction(requestMethod, odataPath, context, actionLookup));
        }
コード例 #3
0
        private static void AddLinkInfoToRouteData(TestControllerContext controllerContext, ODataPath odataPath)
        {
            KeySegment keyValueSegment = odataPath.Segments.OfType <KeySegment>().First();

            controllerContext.AddKeyValueToRouteData(keyValueSegment);

            KeySegment relatedKeySegment = odataPath.Segments.Last() as KeySegment;

            if (relatedKeySegment != null)
            {
                controllerContext.AddKeyValueToRouteData(relatedKeySegment, ODataRouteConstants.RelatedKey);
            }
        }
コード例 #4
0
 protected abstract string SelectAction(string requestMethod, ODataPath odataPath, TestControllerContext controllerContext, IList <string> actionMap);
コード例 #5
0
        /// <inheritdoc/>
        protected override string SelectAction(string requestMethod, ODataPath odataPath, TestControllerContext controllerContext, IList <string> actionList)
        {
            if (odataPath.PathTemplate == "~/entityset/key/property" || odataPath.PathTemplate == "~/entityset/key/cast/property")
            {
                var segment     = odataPath.Segments.Last() as PropertySegment;
                var property    = segment.Property;
                var declareType = property.DeclaringType as IEdmEntityType;
                if (declareType != null)
                {
                    var key = odataPath.Segments[1] as KeySegment;
                    controllerContext.AddKeyValueToRouteData(key);
                    string prefix = ODataHelper.GetHttpPrefix(requestMethod);
                    if (string.IsNullOrEmpty(prefix))
                    {
                        return(null);
                    }
                    string action = prefix + property.Name + "From" + declareType.Name;
                    return(actionList.Contains(action) ? action : prefix + property.Name);
                }
            }

            return(null);
        }
コード例 #6
0
        /// <inheritdoc/>
        protected override string SelectAction(string requestMethod, ODataPath odataPath, TestControllerContext controllerContext, IList <string> actionList)
        {
            if (odataPath.PathTemplate == "~/entityset/key/navigation/$ref" ||
                odataPath.PathTemplate == "~/entityset/key/cast/navigation/$ref" ||
                odataPath.PathTemplate == "~/entityset/key/navigation/key/$ref" ||
                odataPath.PathTemplate == "~/entityset/key/cast/navigation/key/$ref")
            {
                var actionName = string.Empty;
                if ((requestMethod == "POST") || (requestMethod == "PUT"))
                {
                    actionName += "CreateRefTo";
                }
                else if (requestMethod == "DELETE")
                {
                    actionName += "DeleteRefTo";
                }
                else
                {
                    return(null);
                }
                var navigationSegment = odataPath.Segments.OfType <NavigationPropertyLinkSegment>().Last();
                actionName += navigationSegment.NavigationProperty.Name;

                var castSegment = odataPath.Segments[2] as TypeSegment;

                if (castSegment != null)
                {
                    IEdmType elementType = castSegment.EdmType;
                    if (castSegment.EdmType.TypeKind == EdmTypeKind.Collection)
                    {
                        elementType = ((IEdmCollectionType)castSegment.EdmType).ElementType.Definition;
                    }

                    var actionCastName = string.Format("{0}On{1}", actionName, ((IEdmEntityType)elementType).Name);
                    if (actionList.Contains(actionCastName))
                    {
                        AddLinkInfoToRouteData(controllerContext, odataPath);
                        return(actionCastName);
                    }
                }

                if (actionList.Contains(actionName))
                {
                    AddLinkInfoToRouteData(controllerContext, odataPath);
                    return(actionName);
                }
            }
            return(null);
        }
コード例 #7
0
 /// <inheritdoc/>
 protected override string SelectAction(string requestMethod, ODataPath odataPath, TestControllerContext controllerContext, IList <string> actionList)
 {
     if ((odataPath.PathTemplate == "~/entityset/key/navigation") || (odataPath.PathTemplate == "~/entityset/key/cast/navigation"))
     {
         NavigationPropertySegment segment            = odataPath.Segments.Last <ODataPathSegment>() as NavigationPropertySegment;
         IEdmNavigationProperty    navigationProperty = segment.NavigationProperty;
         IEdmEntityType            declaringType      = navigationProperty.DeclaringType as IEdmEntityType;
         if (declaringType != null)
         {
             string prefix = ODataHelper.GetHttpPrefix(requestMethod);
             if (string.IsNullOrEmpty(prefix))
             {
                 return(null);
             }
             KeySegment segment2 = odataPath.Segments[1] as KeySegment;
             controllerContext.AddKeyValueToRouteData(segment2);
             string key = prefix + navigationProperty.Name + "On" + declaringType.Name;
             return(actionList.Contains(key) ? key : (prefix + navigationProperty.Name));
         }
     }
     return(null);
 }