Beispiel #1
0
        public string GetAbsolutePath(string executingFilePath, string pagePath)
        {
            if (string.IsNullOrEmpty(pagePath))
            {
                // Path is not valid; no change required.
                return(pagePath);
            }

            if (IsApplicationRelativePath(pagePath))
            {
                // An absolute path already; no change required.
                return(pagePath);
            }

            if (!IsRelativePath(pagePath))
            {
                // A page name; no change required.
                return(pagePath);
            }

            if (string.IsNullOrEmpty(executingFilePath))
            {
                // Given a relative path i.e. not yet application-relative (starting with "~/" or "/"), interpret
                // path relative to currently-executing view, if any.
                // Not yet executing a view. Start in app root.
                var absolutePath = "/" + pagePath;
                return(ViewEnginePath.ResolvePath(absolutePath));
            }

            return(ViewEnginePath.CombinePath(executingFilePath, pagePath));
        }
    public void ResolvePath_DoesNotModifyPathsWithoutTraversals(string input)
    {
        // Arrange & Act
        var result = ViewEnginePath.ResolvePath(input);

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

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

        // Assert
        Assert.Equal(expected, result);
    }
Beispiel #5
0
    private ViewLocationCacheResult OnCacheMiss(
        ViewLocationExpanderContext expanderContext,
        ViewLocationCacheKey cacheKey)
    {
        var viewLocations = GetViewLocationFormats(expanderContext);

        var expanders = _options.ViewLocationExpanders;
        // Read interface .Count once rather than per iteration
        var expandersCount = expanders.Count;

        for (var i = 0; i < expandersCount; i++)
        {
            viewLocations = expanders[i].ExpandViewLocations(expanderContext, viewLocations);
        }

        ViewLocationCacheResult?cacheResult = null;
        var searchedLocations = new List <string>();
        var expirationTokens  = new HashSet <IChangeToken>();

        foreach (var location in viewLocations)
        {
            var path = string.Format(
                CultureInfo.InvariantCulture,
                location,
                expanderContext.ViewName,
                expanderContext.ControllerName,
                expanderContext.AreaName);

            path = ViewEnginePath.ResolvePath(path);

            cacheResult = CreateCacheResult(expirationTokens, path, expanderContext.IsMainPage);
            if (cacheResult != null)
            {
                break;
            }

            searchedLocations.Add(path);
        }

        // No views were found at the specified location. Create a not found result.
        if (cacheResult == null)
        {
            cacheResult = new ViewLocationCacheResult(searchedLocations);
        }

        var cacheEntryOptions = new MemoryCacheEntryOptions();

        cacheEntryOptions.SetSlidingExpiration(_cacheExpirationDuration);
        foreach (var expirationToken in expirationTokens)
        {
            cacheEntryOptions.AddExpirationToken(expirationToken);
        }

        ViewLookupCache.Set(cacheKey, cacheResult, cacheEntryOptions);
        return(cacheResult);
    }
Beispiel #6
0
        public static bool IsCurrentPage(this ViewContext context, string pageName)
        {
            string    normalizedRouteValue = NormalizedRouteValue.GetNormalizedRouteValue(context, "page");
            string    expectedPageValue    = ViewEnginePath.CombinePath(normalizedRouteValue, pageName);
            RouteData routeData            = context.HttpContext.GetRouteData();
            var       page = (string)routeData.Values["page"];

            if (page == expectedPageValue)
            {
                return(true);
            }

            return(false);
        }
Beispiel #7
0
        private static object CalculatePageName(ActionContext actionContext, 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] != '/')
            {
                var currentPagePath = NormalizedRouteValue.GetNormalizedRouteValue(actionContext, "page");
                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);
        }
Beispiel #8
0
        private ViewLocationCacheResult OnCacheMiss(
            ViewLocationExpanderContext expanderContext,
            ViewLocationCacheKey cacheKey)
        {
            var sb = new StringBuilder();

            sb.Append(expanderContext.ViewName)
            .Append(expanderContext.ControllerName)
            .Append(expanderContext.AreaName);

            var key = sb.ToString();

            if (!_viewLocations1.ContainsKey(key))
            {
                var viewLocations = GetViewLocationFormats(expanderContext);
                for (var i = 0; i < _options.ViewLocationExpanders.Count; i++)
                {
                    viewLocations = _options.ViewLocationExpanders[i]
                                    .ExpandViewLocations(expanderContext, viewLocations);
                }

                var resolvedLocations = new List <string>();
                foreach (var location in viewLocations)
                {
                    var path = string.Format(
                        CultureInfo.InvariantCulture,
                        location,
                        expanderContext.ViewName,
                        expanderContext.ControllerName,
                        expanderContext.AreaName);

                    path = ViewEnginePath.ResolvePath(path);
                    resolvedLocations.Add(path);
                }

                _viewLocations1[key] = resolvedLocations;
            }

            ViewLocationCacheResult cacheResult = null;
            var searchedLocations = new List <string>();
            var expirationTokens  = new HashSet <IChangeToken>();

            foreach (var path in _viewLocations1[key])
            {
                cacheResult = CreateCacheResult(expirationTokens, path, expanderContext.IsMainPage);
                if (cacheResult != null)
                {
                    break;
                }

                searchedLocations.Add(path);
            }

            // No views were found at the specified location. Create a not found result.
            if (cacheResult == null)
            {
                cacheResult = new ViewLocationCacheResult(searchedLocations);
            }

            var cacheEntryOptions = new MemoryCacheEntryOptions();

            cacheEntryOptions.SetSlidingExpiration(_cacheExpirationDuration);
            //foreach (var expirationToken in expirationTokens)
            //{
            //    cacheEntryOptions.AddExpirationToken(expirationToken);
            //}

            return(ViewLookupCache.Set(cacheKey, cacheResult, cacheEntryOptions));
        }