Example #1
0
        private static object CalculatePageName(ActionContext context, RouteValueDictionary ambientValues, string pageName)
        {
            Debug.Assert(pageName.Length > 0);
            // Paths not qualified with a leading slash are treated as relative to the current page.
            if (pageName[0] != '/')
            {
                // OK now we should get the best 'normalized' version of the page route value that we can.
                string currentPagePath;
                if (context != null)
                {
                    currentPagePath = NormalizedRouteValue.GetNormalizedRouteValue(context, "page");
                }
                else if (ambientValues != null)
                {
                    currentPagePath = ambientValues["page"]?.ToString();
                }
                else
                {
                    currentPagePath = null;
                }

                if (string.IsNullOrEmpty(currentPagePath))
                {
                    // Disallow the use sibling page routing, a Razor page specific feature, from a non-page action.
                    throw new InvalidOperationException(Resources.FormatUrlHelper_RelativePagePathIsNotSupported(pageName));
                }

                return(ViewEnginePath.CombinePath(currentPagePath, pageName));
            }

            return(pageName);
        }
Example #2
0
        public void ResolvePath_DoesNotModifyPathsWithoutTraversals(string input)
        {
            // Arrange & Act
            var result = ViewEnginePath.ResolvePath(input);

            // Assert
            Assert.Same(input, result);
        }
Example #3
0
        public void ResolvePath_DoesNotTraversePastApplicationRoot(string input)
        {
            // Arrange
            var result = ViewEnginePath.ResolvePath(input);

            // Assert
            Assert.Same(input, result);
        }
Example #4
0
        public void ResolvePath_ResolvesPathTraversals(string input, string expected)
        {
            // Arrange & Act
            var result = ViewEnginePath.ResolvePath(input);

            // Assert
            Assert.Equal(expected, result);
        }
Example #5
0
        private static object CalculatePageName(ActionContext context, RouteValueDictionary ambientValues, string pageName)
        {
            Debug.Assert(pageName.Length > 0);
            // Paths not qualified with a leading slash are treated as relative to the current page.
            if (pageName[0] != '/')
            {
                // OK now we should get the best 'normalized' version of the page route value that we can.
                string currentPagePath;
                if (context != null)
                {
                    currentPagePath = NormalizedRouteValue.GetNormalizedRouteValue(context, "page");
                }
                else if (ambientValues != null)
                {
                    currentPagePath = Convert.ToString(ambientValues["page"], CultureInfo.InvariantCulture);
                }
                else
                {
                    currentPagePath = null;
                }

                if (string.IsNullOrEmpty(currentPagePath))
                {
                    // Disallow the use sibling page routing, a Razor page specific feature, from a non-page action.
                    // OR - this is a call from LinkGenerator where the HttpContext was not specified.
                    //
                    // We can't use a relative path in either case, because we don't know the base path.
                    throw new InvalidOperationException(Resources.FormatUrlHelper_RelativePagePathIsNotSupported(
                                                            pageName,
                                                            nameof(LinkGenerator),
                                                            nameof(HttpContext)));
                }

                return(ViewEnginePath.CombinePath(currentPagePath, pageName));
            }

            return(pageName);
        }