using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.Text; namespace Microsoft.AspNetCore.SpaServices.Prerendering { /// /// Describes the prerendering result returned by JavaScript code. /// public class RenderToStringResult { /// /// If set, specifies JSON-serializable data that should be added as a set of global JavaScript variables in the document. /// This can be used to transfer arbitrary data from server-side prerendering code to client-side code (for example, to /// transfer the state of a Redux store). /// public JObject Globals { get; set; } /// /// The HTML generated by the prerendering logic. /// public string Html { get; set; } /// /// If set, specifies that instead of rendering HTML, the response should be an HTTP redirection to this URL. /// This can be used if the prerendering code determines that the requested URL would lead to a redirection according /// to the SPA's routing configuration. /// public string RedirectUrl { get; set; } /// /// If set, specifies the HTTP status code that should be sent back with the server response. /// public int? StatusCode { get; set; } /// /// Constructs a block of JavaScript code that assigns data from the /// property to the global namespace. /// /// A block of JavaScript code. public string CreateGlobalsAssignmentScript() { if (Globals == null) { return string.Empty; } var stringBuilder = new StringBuilder(); foreach (var property in Globals.Properties()) { stringBuilder.AppendFormat("window.{0} = {1};", property.Name, property.Value.ToString(Formatting.None)); } return stringBuilder.ToString(); } } }