11using System ;
2- using System . Text ;
32using System . Threading ;
43using System . Threading . Tasks ;
54using Microsoft . AspNetCore . Hosting ;
6- using Microsoft . AspNetCore . Http . Features ;
75using Microsoft . AspNetCore . Mvc . ViewFeatures ;
86using Microsoft . AspNetCore . Mvc . Rendering ;
97using Microsoft . AspNetCore . NodeServices ;
108using Microsoft . AspNetCore . Razor . TagHelpers ;
11- using Newtonsoft . Json ;
129
1310namespace Microsoft . AspNetCore . SpaServices . Prerendering
1411{
@@ -90,19 +87,6 @@ public PrerenderTagHelper(IServiceProvider serviceProvider)
9087 /// <returns>A <see cref="Task"/> representing the operation.</returns>
9188 public override async Task ProcessAsync ( TagHelperContext context , TagHelperOutput output )
9289 {
93- // We want to pass the original, unencoded incoming URL data through to Node, so that
94- // server-side code has the same view of the URL as client-side code (on the client,
95- // location.pathname returns an unencoded string).
96- // The following logic handles special characters in URL paths in the same way that
97- // Node and client-side JS does. For example, the path "/a=b%20c" gets passed through
98- // unchanged (whereas other .NET APIs do change it - Path.Value will return it as
99- // "/a=b c" and Path.ToString() will return it as "/a%3db%20c")
100- var requestFeature = ViewContext . HttpContext . Features . Get < IHttpRequestFeature > ( ) ;
101- var unencodedPathAndQuery = requestFeature . RawTarget ;
102-
103- var request = ViewContext . HttpContext . Request ;
104- var unencodedAbsoluteUrl = $ "{ request . Scheme } ://{ request . Host } { unencodedPathAndQuery } ";
105-
10690 var result = await Prerenderer . RenderToString (
10791 _applicationBasePath ,
10892 _nodeServices ,
@@ -111,11 +95,9 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
11195 {
11296 ExportName = ExportName
11397 } ,
114- unencodedAbsoluteUrl ,
115- unencodedPathAndQuery ,
98+ ViewContext . HttpContext ,
11699 CustomDataParameter ,
117- TimeoutMillisecondsParameter ,
118- request . PathBase . ToString ( ) ) ;
100+ TimeoutMillisecondsParameter ) ;
119101
120102 if ( ! string . IsNullOrEmpty ( result . RedirectUrl ) )
121103 {
@@ -134,19 +116,10 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
134116
135117 // Also attach any specified globals to the 'window' object. This is useful for transferring
136118 // general state between server and client.
137- if ( result . Globals != null )
119+ var globalsScript = result . CreateGlobalsAssignmentScript ( ) ;
120+ if ( ! string . IsNullOrEmpty ( globalsScript ) )
138121 {
139- var stringBuilder = new StringBuilder ( ) ;
140- foreach ( var property in result . Globals . Properties ( ) )
141- {
142- stringBuilder . AppendFormat ( "window.{0} = {1};" ,
143- property . Name ,
144- property . Value . ToString ( Formatting . None ) ) ;
145- }
146- if ( stringBuilder . Length > 0 )
147- {
148- output . PostElement . SetHtmlContent ( $ "<script>{ stringBuilder } </script>") ;
149- }
122+ output . PostElement . SetHtmlContent ( $ "<script>{ globalsScript } </script>") ;
150123 }
151124 }
152125 }
0 commit comments