コード例 #1
0
        public virtual string ExtractFromKey(Type type)
        {
            //Check if type is a controller without a FromAction
            if (type.IsController() && string.IsNullOrWhiteSpace(FromAction) && FromController != null)
            {
                //should only come here if type is a controller and attribute is placed at controller class level
                return(FromController.ExtractMvcControllerKey());
            }

            if (!string.IsNullOrWhiteSpace(FromAction))
            {
                var fromControllerType = FromController;

                if (fromControllerType == null)
                {
                    // Try to infer it from the type
                    if (!type.IsController())
                    {
                        throw new SmartBreadcrumbsException($"Can't infer FromController as '{type.Name}' is a Razor Page.");
                    }

                    fromControllerType = type;
                }

                if (!fromControllerType.IsController())
                {
                    throw new SmartBreadcrumbsException($"'{fromControllerType.Name}' is used in FromController but isn't a Controller.");
                }

                var actionMethod = fromControllerType.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                                   .FirstOrDefault(m => m.Name == FromAction); //to prevent AmbiguousMatchException

                if (actionMethod == null || actionMethod.ReturnType.IsAction() == false)
                {
                    throw new SmartBreadcrumbsException($"{fromControllerType.Name} doesn't contain a valid action named {FromAction}.");
                }

                return(fromControllerType.ExtractMvcKey(actionMethod));
            }

            if (FromPage == null)
            {
                return(null); // Will use default as parent
            }
            if (!FromPage.IsRazorPage())
            {
                throw new SmartBreadcrumbsException($"'{FromPage.Name}' is used in FromPage but isn't a Razor Page.");
            }

            return(FromPage.ExtractRazorPageKey());
        }
コード例 #2
0
        public virtual string ExtractFromKey(Type type)
        {
            if (!string.IsNullOrWhiteSpace(FromAction))
            {
                var fromControllerType = FromController;

                if (fromControllerType == null)
                {
                    // Try to infer it from the type
                    if (!type.IsController())
                    {
                        throw new SmartBreadcrumbsException($"Can't infer FromController as '{type.Name}' is a Razor Page.");
                    }

                    fromControllerType = type;
                }

                if (!fromControllerType.IsController())
                {
                    throw new SmartBreadcrumbsException($"'{fromControllerType.Name}' is used in FromController but isn't a Controller.");
                }

                var actionMethod = fromControllerType.GetMethod(FromAction, BindingFlags.Instance | BindingFlags.Public);
                if (actionMethod == null || actionMethod.ReturnType.IsAction() == false)
                {
                    throw new SmartBreadcrumbsException($"{fromControllerType.Name} doesn't contain a valid action named {FromAction}.");
                }

                return(fromControllerType.ExtractMvcKey(actionMethod));
            }

            if (FromPage == null)
            {
                return(null); // Will use default as parent
            }
            if (!FromPage.IsRazorPage())
            {
                throw new SmartBreadcrumbsException($"'{FromPage.Name}' is used in FromPage but isn't a Razor Page.");
            }

            return(FromPage.ExtractRazorPageKey());
        }