1
1
using System ;
2
- using System . Text ;
3
2
using System . Threading ;
4
3
using System . Threading . Tasks ;
5
4
using Microsoft . AspNetCore . Hosting ;
6
- using Microsoft . AspNetCore . Http . Features ;
7
5
using Microsoft . AspNetCore . Mvc . ViewFeatures ;
8
6
using Microsoft . AspNetCore . Mvc . Rendering ;
9
7
using Microsoft . AspNetCore . NodeServices ;
10
8
using Microsoft . AspNetCore . Razor . TagHelpers ;
11
- using Newtonsoft . Json ;
12
9
13
10
namespace Microsoft . AspNetCore . SpaServices . Prerendering
14
11
{
@@ -90,19 +87,6 @@ public PrerenderTagHelper(IServiceProvider serviceProvider)
90
87
/// <returns>A <see cref="Task"/> representing the operation.</returns>
91
88
public override async Task ProcessAsync ( TagHelperContext context , TagHelperOutput output )
92
89
{
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
-
106
90
var result = await Prerenderer . RenderToString (
107
91
_applicationBasePath ,
108
92
_nodeServices ,
@@ -111,11 +95,9 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
111
95
{
112
96
ExportName = ExportName
113
97
} ,
114
- unencodedAbsoluteUrl ,
115
- unencodedPathAndQuery ,
98
+ ViewContext . HttpContext ,
116
99
CustomDataParameter ,
117
- TimeoutMillisecondsParameter ,
118
- request . PathBase . ToString ( ) ) ;
100
+ TimeoutMillisecondsParameter ) ;
119
101
120
102
if ( ! string . IsNullOrEmpty ( result . RedirectUrl ) )
121
103
{
@@ -134,19 +116,10 @@ public override async Task ProcessAsync(TagHelperContext context, TagHelperOutpu
134
116
135
117
// Also attach any specified globals to the 'window' object. This is useful for transferring
136
118
// general state between server and client.
137
- if ( result . Globals != null )
119
+ var globalsScript = result . CreateGlobalsAssignmentScript ( ) ;
120
+ if ( ! string . IsNullOrEmpty ( globalsScript ) )
138
121
{
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>") ;
150
123
}
151
124
}
152
125
}
0 commit comments