RenderToString() public static method

public static RenderToString ( string applicationBasePath, INodeServices nodeServices, JavaScriptModuleExport bootModule, string requestAbsoluteUrl, string requestPathAndQuery, object customDataParameter ) : Task
applicationBasePath string
nodeServices INodeServices
bootModule JavaScriptModuleExport
requestAbsoluteUrl string
requestPathAndQuery string
customDataParameter object
return Task
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            var request = this.ViewContext.HttpContext.Request;
            var result  = await Prerenderer.RenderToString(
                applicationBasePath : this.applicationBasePath,
                nodeServices : this.nodeServices,
                bootModule : new JavaScriptModuleExport(this.ModuleName)
            {
                exportName    = this.ExportName,
                webpackConfig = this.WebpackConfigPath
            },
                requestAbsoluteUrl : UriHelper.GetEncodedUrl(request),
                requestPathAndQuery : request.Path + request.QueryString.Value);

            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.
            if (result.Globals != null)
            {
                var stringBuilder = new StringBuilder();
                foreach (var property in result.Globals.Properties())
                {
                    stringBuilder.AppendFormat("window.{0} = {1};",
                                               property.Name,
                                               property.Value.ToString(Formatting.None));
                }
                if (stringBuilder.Length > 0)
                {
                    output.PostElement.SetHtmlContent($"<script>{ stringBuilder.ToString() }</script>");
                }
            }
        }
Beispiel #2
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));
 }
Beispiel #3
0
        /// <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))
            {
                var permanentRedirect = result.StatusCode == 301;

                // It's a redirection
                ViewContext.HttpContext.Response.Redirect(result.RedirectUrl, permanentRedirect);
                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>");
            }
        }