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>"); } } }
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)) { 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>"); } }