Exemplo n.º 1
0
 public Task <RenderToStringResult> RenderToString(
     string moduleName,
     string exportName          = null,
     object customDataParameter = null,
     int timeoutMilliseconds    = default(int))
 {
     return(Prerenderer.RenderToString(
                _applicationBasePath,
                _nodeServices,
                _applicationStoppingToken,
                new JavaScriptModuleExport(moduleName)
     {
         ExportName = exportName
     },
                _httpContextAccessor.HttpContext,
                customDataParameter,
                timeoutMilliseconds));
 }
        /// <summary>
        /// Executes the tag helper to perform server-side prerendering.
        /// </summary>
        /// <param name="context">The <see cref="TagHelperContext"/>.</param>
        /// <param name="output">The <see cref="TagHelperOutput"/>.</param>
        /// <returns>A <see cref="Task"/> representing the operation.</returns>
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var result = await Prerenderer.RenderToString(
                _applicationBasePath,
                _nodeServices,
                _applicationStoppingToken,
                new JavaScriptModuleExport(ModuleName)
            {
                ExportName = ExportName
            },
                ViewContext.HttpContext,
                CustomDataParameter,
                TimeoutMillisecondsParameter);

            if (!string.IsNullOrEmpty(result.RedirectUrl))
            {
                // It's a redirection
                ViewContext.HttpContext.Response.Redirect(result.RedirectUrl);
                return;
            }

            if (result.StatusCode.HasValue)
            {
                ViewContext.HttpContext.Response.StatusCode = result.StatusCode.Value;
            }

            // It's some HTML to inject
            output.Content.SetHtmlContent(result.Html);

            // Also attach any specified globals to the 'window' object. This is useful for transferring
            // general state between server and client.
            var globalsScript = result.CreateGlobalsAssignmentScript();

            if (!string.IsNullOrEmpty(globalsScript))
            {
                output.PostElement.SetHtmlContent($"<script>{globalsScript}</script>");
            }
        }