public async Task <string> RenderPageAsync <T>(T model, string path, string area = null)
        {
            var actionContext = CreateActionContext(path, area ?? string.Empty);
            var page          = _engine.FindPage(actionContext, path).Page as Page;

            if (page == null)
            {
                throw new ArgumentException($"Unable to find page {path}");
            }
            var view = CreateView(page);

            using (var writer = new StringWriter())
            {
                var viewContext = CreateViewContext(actionContext, view, model, writer);
                page.PageContext = new PageContext
                {
                    ViewData = viewContext.ViewData
                };
                page.ViewContext = viewContext;
                _activator.Activate(page, viewContext);
                await page.ExecuteAsync();

                var rendered = writer.ToString();
                return(rendered);
            }
        }
Exemplo n.º 2
0
 /// <inheritdoc />
 public new Task RenderAsync(ViewContext context)
 {
     _pageActivator.Activate(RazorPage, context);
     if (RazorPage == null)
     {
         throw new InvalidOperationException("无效视图类型");
     }
     return(RazorPage.ExecuteAsync());
 }
        public async Task <string> RenderToStringAsync <T>(string pageName, string pageLayout, T model) where T : PageModel
        {
            var actionContext =
                new ActionContext(
                    _httpContext.HttpContext,
                    _httpContext.HttpContext.GetRouteData(),
                    _actionContext.ActionContext.ActionDescriptor
                    );

            using (var sw = new StringWriter())
            {
                var result = _razorViewEngine.FindPage(actionContext, pageName);

                if (result.Page == null)
                {
                    throw new ArgumentNullException($"The page {pageName} cannot be found.");
                }

                var page = ((Page)result.Page);
                page.Layout = pageLayout;

                var view = new RazorView(_razorViewEngine,
                                         _activator,
                                         new List <IRazorPage>(),
                                         page,
                                         HtmlEncoder.Default,
                                         new DiagnosticListener("ViewRenderService"));

                var viewContext = new ViewContext(
                    actionContext,
                    view,
                    new ViewDataDictionary <T>(new EmptyModelMetadataProvider(), new ModelStateDictionary())
                {
                    Model = model
                },
                    new TempDataDictionary(
                        _httpContext.HttpContext,
                        _tempDataProvider
                        ),
                    sw,
                    new HtmlHelperOptions()
                    );

                page.PageContext = new Microsoft.AspNetCore.Mvc.RazorPages.PageContext
                {
                    ViewData = viewContext.ViewData
                };

                page.ViewContext = viewContext;

                _activator.Activate(page, viewContext);

                await page.ExecuteAsync();

                return(sw.ToString());
            }
        }
Exemplo n.º 4
0
        public static async Task <string> RenderViewAsync(this PageModel pageModel, string pageName)
        {
            var actionContext = new ActionContext(
                pageModel.HttpContext,
                pageModel.RouteData,
                pageModel.PageContext.ActionDescriptor
                );

            using (var sw = new StringWriter())
            {
                IRazorViewEngine    _razorViewEngine = pageModel.HttpContext.RequestServices.GetService(typeof(IRazorViewEngine)) as IRazorViewEngine;
                IRazorPageActivator _activator       = pageModel.HttpContext.RequestServices.GetService(typeof(IRazorPageActivator)) as IRazorPageActivator;

                var result = _razorViewEngine.FindPage(actionContext, pageName);

                if (result.Page == null)
                {
                    throw new ArgumentNullException($"The page {pageName} cannot be found.");
                }

                var page = result.Page;

                var view = new RazorView(_razorViewEngine,
                                         _activator,
                                         new List <IRazorPage>(),
                                         page,
                                         HtmlEncoder.Default,
                                         new DiagnosticListener("ViewRenderService"));


                var viewContext = new ViewContext(
                    actionContext,
                    view,
                    pageModel.ViewData,
                    pageModel.TempData,
                    sw,
                    new HtmlHelperOptions()
                    );


                var pageNormal = ((Page)result.Page);

                pageNormal.PageContext = pageModel.PageContext;

                pageNormal.ViewContext = viewContext;


                _activator.Activate(pageNormal, viewContext);

                await page.ExecuteAsync();

                return(sw.ToString());
            }
        }
Exemplo n.º 5
0
        private async Task RenderPageCoreAsync(IRazorPage page, ViewContext context)
        {
            page.IsPartial   = IsPartial;
            page.ViewContext = context;
            if (EnableInstrumentation)
            {
                page.PageExecutionContext = _pageExecutionFeature.GetContext(page.Path, context.Writer);
            }

            _pageActivator.Activate(page, context);
            await page.ExecuteAsync();
        }
Exemplo n.º 6
0
        private async Task RenderPageCoreAsync(IRazorPage page, ViewContext context)
        {
            page.ViewContext = context;
            _pageActivator.Activate(page, context);

            _diagnosticSource.BeforeViewPage(page, context);

            try
            {
                await page.ExecuteAsync();
            }
            finally
            {
                _diagnosticSource.AfterViewPage(page, context);
            }
        }
        private async Task <string> RenderPageAsString(ActionContext actionContext)
        {
            using var sw = new StringWriter();
            var pageResult = _razorViewEngine.FindPage(actionContext, _razorPageName);;

            if (pageResult.Page == null)
            {
                throw new ArgumentNullException($"The page {_razorPageName} cannot be found.");
            }
            var viewContext = GetViewContext(actionContext, pageResult.Page, sw);
            var page        = (Page)pageResult.Page;

            page.PageContext = PageModel.PageContext;
            page.ViewContext = viewContext;
            _activator.Activate(page, viewContext);
            await page.ExecuteAsync();

            return(sw.ToString());
        }
Exemplo n.º 8
0
        public async Task <string> RenderToStringAsync <T>(string pageName, T model)
            where T : PageModel
        {
            var actionContext = new ActionContext(httpContext.HttpContext, httpContext.HttpContext.GetRouteData(), this.actionContextAccessor.ActionContext.ActionDescriptor);

            await using var writer = new StringWriter();
            var result = razorViewEngine.FindPage(actionContext, pageName);

            if (result.Page == null)
            {
                throw new ArgumentNullException($"The page {pageName} cannot be found.");
            }

            using var listener = new DiagnosticListener("ViewRenderService");
            var view = new RazorView(razorViewEngine, activator, new List <IRazorPage>(), result.Page, HtmlEncoder.Default, listener);
            var viewDataDictionary =
                new ViewDataDictionary <T>(new EmptyModelMetadataProvider(), new ModelStateDictionary())
            {
                Model = model
            };
            var viewContext = new ViewContext(
                actionContext,
                view,
                viewDataDictionary,
                new TempDataDictionary(httpContext.HttpContext, tempDataProvider),
                writer,
                new HtmlHelperOptions());

            var page = (Page)result.Page;

            page.PageContext = new PageContext
            {
                ViewData = viewContext.ViewData
            };

            page.ViewContext = viewContext;

            activator.Activate(page, viewContext);

            await page.ExecuteAsync().ConfigureAwait(false);

            return(writer.ToString());
        }
Exemplo n.º 9
0
        public IRazorPage CreateRazorPage(string helpersViewPath, ViewContext viewContext)
        {
            var viewEngineResult = _viewEngine.GetView(viewContext.ExecutingFilePath, helpersViewPath, isMainPage: false);

            var originalLocations = viewEngineResult.SearchedLocations;

            if (!viewEngineResult.Success)
            {
                viewEngineResult = _viewEngine.FindView(viewContext, helpersViewPath, isMainPage: false);
            }

            if (!viewEngineResult.Success)
            {
                var locations = string.Empty;

                if (originalLocations.Any())
                {
                    locations = Environment.NewLine + string.Join(Environment.NewLine, originalLocations);
                }

                if (viewEngineResult.SearchedLocations.Any())
                {
                    locations += Environment.NewLine + string.Join(Environment.NewLine, viewEngineResult.SearchedLocations);
                }

                throw new InvalidOperationException($"The Razor helpers view '{helpersViewPath}' was not found. The following locations were searched:{locations}");
            }

            var razorPage = ((RazorView)viewEngineResult.View).RazorPage;

            razorPage.ViewContext = viewContext;

            // we need to save and restore the original view data dictionary as it is changed by IRazorPageActivator.Activate
            // https://github.com/dotnet/aspnetcore/blob/v3.1.6/src/Mvc/Mvc.Razor/src/RazorPagePropertyActivator.cs#L59
            var originalViewData = viewContext.ViewData;

            try { _razorPageActivator.Activate(razorPage, viewContext); }
            finally { viewContext.ViewData = originalViewData; }

            return(razorPage);
        }
Exemplo n.º 10
0
 private async Task RenderPageCoreAsync(IRazorPage page, ViewContext context)
 {
     page.ViewContext = context;
     _pageActivator.Activate(page, context);
     await page.ExecuteAsync();
 }
Exemplo n.º 11
0
 private Task RenderPageCoreAsync(IRazorPage page, ViewContext context)
 {
     page.ViewContext = context;
     _pageActivator.Activate(page, context);
     return(page.ExecuteAsync());
 }