Example #1
0
        private RazorPageResult GetRazorPageResult(
            ActionContext context,
            string pageName,
            bool isPartial)
        {
            if (IsApplicationRelativePath(pageName))
            {
                var applicationRelativePath = pageName;
                if (!pageName.EndsWith(ViewExtension, StringComparison.OrdinalIgnoreCase))
                {
                    applicationRelativePath += ViewExtension;
                }

                var page = _pageFactory.CreateInstance(applicationRelativePath);
                if (page != null)
                {
                    return(new RazorPageResult(pageName, page));
                }

                return(new RazorPageResult(pageName, new[] { pageName }));
            }
            else
            {
                return(LocatePageFromViewLocations(context, pageName, isPartial));
            }
        }
Example #2
0
        private async Task RenderLayoutAsync(ViewContext context,
                                             RazorTextWriter bodyWriter)
        {
            // A layout page can specify another layout page. We'll need to continue
            // looking for layout pages until they're no longer specified.
            var previousPage = _page;

            while (!string.IsNullOrEmpty(previousPage.Layout))
            {
                var layoutPage = _pageFactory.CreateInstance(previousPage.Layout);
                if (layoutPage == null)
                {
                    var message = Resources.FormatLayoutCannotBeLocated(previousPage.Layout);
                    throw new InvalidOperationException(message);
                }

                layoutPage.PreviousSectionWriters = previousPage.SectionWriters;
                layoutPage.RenderBodyDelegate     = bodyWriter.CopyTo;
                bodyWriter = await RenderPageAsync(layoutPage, context, executeViewStart : false);

                // Verify that RenderBody is called, or that RenderSection is called for all sections
                layoutPage.EnsureBodyAndSectionsWereRendered();

                previousPage = layoutPage;
            }

            await bodyWriter.CopyToAsync(context.Writer);
        }
Example #3
0
        private ViewEngineResult CreateViewEngineResult(ActionContext context,
                                                        string viewName,
                                                        bool partial)
        {
            var nameRepresentsPath = IsSpecificPath(viewName);

            if (nameRepresentsPath)
            {
                if (!viewName.EndsWith(ViewExtension, StringComparison.OrdinalIgnoreCase))
                {
                    throw new InvalidOperationException(
                              Resources.FormatViewMustEndInExtension(viewName, ViewExtension));
                }

                var page = _pageFactory.CreateInstance(viewName);

                return(page != null?CreateFoundResult(page, viewName, partial) :
                           ViewEngineResult.NotFound(viewName, new[] { viewName }));
            }
            else
            {
                var routeValues    = context.RouteData.Values;
                var controllerName = routeValues.GetValueOrDefault <string>("controller");
                var areaName       = routeValues.GetValueOrDefault <string>("area");
                var potentialPaths = GetViewSearchPaths(viewName, controllerName, areaName);

                foreach (var path in potentialPaths)
                {
                    var page = _pageFactory.CreateInstance(path);
                    if (page != null)
                    {
                        return(CreateFoundResult(page, path, partial));
                    }
                }

                return(ViewEngineResult.NotFound(viewName, potentialPaths));
            }
        }
Example #4
0
        private ViewEngineResult CreateViewEngineResult(ActionContext context,
                                                        string viewName,
                                                        bool partial)
        {
            var nameRepresentsPath = IsSpecificPath(viewName);

            if (nameRepresentsPath)
            {
                if (viewName.EndsWith(ViewExtension, StringComparison.OrdinalIgnoreCase))
                {
                    var page = _pageFactory.CreateInstance(viewName);
                    if (page != null)
                    {
                        return(CreateFoundResult(context, page, viewName, partial));
                    }
                }
                return(ViewEngineResult.NotFound(viewName, new[] { viewName }));
            }
            else
            {
                return(LocateViewFromViewLocations(context, viewName, partial));
            }
        }
Example #5
0
        private async Task RenderLayoutAsync(ViewContext context,
                                             RazorTextWriter bodyWriter)
        {
            // A layout page can specify another layout page. We'll need to continue
            // looking for layout pages until they're no longer specified.
            var previousPage = _razorPage;

            while (!string.IsNullOrEmpty(previousPage.Layout))
            {
                if (!bodyWriter.IsBuffering)
                {
                    // Once a call to RazorPage.FlushAsync is made, we can no longer render Layout pages - content has
                    // already been written to the client and the layout content would be appended rather than surround
                    // the body content. Throwing this exception wouldn't return a 500 (since content has already been
                    // written), but a diagnostic component should be able to capture it.

                    var message = Resources.FormatLayoutCannotBeRendered("FlushAsync");
                    throw new InvalidOperationException(message);
                }

                var layoutPage = _pageFactory.CreateInstance(previousPage.Layout);
                if (layoutPage == null)
                {
                    var message = Resources.FormatLayoutCannotBeLocated(previousPage.Layout);
                    throw new InvalidOperationException(message);
                }

                // Notify the previous page that any writes that are performed on it are part of sections being written
                // in the layout.
                previousPage.IsLayoutBeingRendered = true;
                layoutPage.PreviousSectionWriters  = previousPage.SectionWriters;
                layoutPage.RenderBodyDelegate      = bodyWriter.CopyTo;
                bodyWriter = await RenderPageAsync(layoutPage, context, executeViewStart : false);

                // Verify that RenderBody is called, or that RenderSection is called for all sections
                layoutPage.EnsureBodyAndSectionsWereRendered();

                previousPage = layoutPage;
            }

            if (bodyWriter.IsBuffering)
            {
                // Only copy buffered content to the Output if we're currently buffering.
                await bodyWriter.CopyToAsync(context.Writer);
            }
        }