7
7
8
8
using System ;
9
9
using System . IO ;
10
+ using System . Text ;
10
11
11
12
#if LEGACYASPNET
12
13
using System . Web ;
13
14
using IHtmlHelper = System . Web . Mvc . HtmlHelper ;
14
15
#else
15
16
using Microsoft . AspNetCore . Mvc . Rendering ;
17
+ using Microsoft . AspNetCore . Html ;
16
18
using IHtmlString = Microsoft . AspNetCore . Html . IHtmlContent ;
17
19
#endif
18
20
@@ -27,6 +29,9 @@ namespace React.AspNet
27
29
/// </summary>
28
30
public static class HtmlHelperExtensions
29
31
{
32
+ [ ThreadStatic ]
33
+ private static StringWriter _sharedStringWriter ;
34
+
30
35
/// <summary>
31
36
/// Gets the React environment
32
37
/// </summary>
@@ -66,28 +71,25 @@ public static IHtmlString React<T>(
66
71
IRenderFunctions renderFunctions = null
67
72
)
68
73
{
69
- return new ActionHtmlString ( writer =>
74
+ try
70
75
{
71
- try
76
+ var reactComponent = Environment . CreateComponent ( componentName , props , containerId , clientOnly , serverOnly ) ;
77
+ if ( ! string . IsNullOrEmpty ( htmlTag ) )
72
78
{
73
- var reactComponent = Environment . CreateComponent ( componentName , props , containerId , clientOnly , serverOnly ) ;
74
- if ( ! string . IsNullOrEmpty ( htmlTag ) )
75
- {
76
- reactComponent . ContainerTag = htmlTag ;
77
- }
78
-
79
- if ( ! string . IsNullOrEmpty ( containerClass ) )
80
- {
81
- reactComponent . ContainerClass = containerClass ;
82
- }
83
-
84
- reactComponent . RenderHtml ( writer , clientOnly , serverOnly , exceptionHandler , renderFunctions ) ;
79
+ reactComponent . ContainerTag = htmlTag ;
85
80
}
86
- finally
81
+
82
+ if ( ! string . IsNullOrEmpty ( containerClass ) )
87
83
{
88
- Environment . ReturnEngineToPool ( ) ;
84
+ reactComponent . ContainerClass = containerClass ;
89
85
}
90
- } ) ;
86
+
87
+ return RenderToString ( writer => reactComponent . RenderHtml ( writer , clientOnly , serverOnly , exceptionHandler , renderFunctions ) ) ;
88
+ }
89
+ finally
90
+ {
91
+ Environment . ReturnEngineToPool ( ) ;
92
+ }
91
93
}
92
94
93
95
/// <summary>
@@ -116,31 +118,32 @@ public static IHtmlString ReactWithInit<T>(
116
118
Action < Exception , string , string > exceptionHandler = null
117
119
)
118
120
{
119
- return new ActionHtmlString ( writer =>
121
+ try
120
122
{
121
- try
123
+ var reactComponent = Environment . CreateComponent ( componentName , props , containerId , clientOnly ) ;
124
+ if ( ! string . IsNullOrEmpty ( htmlTag ) )
122
125
{
123
- var reactComponent = Environment . CreateComponent ( componentName , props , containerId , clientOnly ) ;
124
- if ( ! string . IsNullOrEmpty ( htmlTag ) )
125
- {
126
- reactComponent . ContainerTag = htmlTag ;
127
- }
126
+ reactComponent . ContainerTag = htmlTag ;
127
+ }
128
128
129
- if ( ! string . IsNullOrEmpty ( containerClass ) )
130
- {
131
- reactComponent . ContainerClass = containerClass ;
132
- }
129
+ if ( ! string . IsNullOrEmpty ( containerClass ) )
130
+ {
131
+ reactComponent . ContainerClass = containerClass ;
132
+ }
133
133
134
+ return RenderToString ( writer =>
135
+ {
134
136
reactComponent . RenderHtml ( writer , clientOnly , exceptionHandler : exceptionHandler ) ;
135
137
writer . WriteLine ( ) ;
136
138
WriteScriptTag ( writer , bodyWriter => reactComponent . RenderJavaScript ( bodyWriter ) ) ;
137
- }
138
- finally
139
- {
140
- Environment . ReturnEngineToPool ( ) ;
141
- }
142
- } ) ;
143
- }
139
+ } ) ;
140
+
141
+ }
142
+ finally
143
+ {
144
+ Environment . ReturnEngineToPool ( ) ;
145
+ }
146
+ }
144
147
145
148
/// <summary>
146
149
/// Renders the JavaScript required to initialise all components client-side. This will
@@ -149,17 +152,33 @@ public static IHtmlString ReactWithInit<T>(
149
152
/// <returns>JavaScript for all components</returns>
150
153
public static IHtmlString ReactInitJavaScript ( this IHtmlHelper htmlHelper , bool clientOnly = false )
151
154
{
152
- return new ActionHtmlString ( writer =>
155
+ try
153
156
{
154
- try
157
+ return RenderToString ( writer =>
155
158
{
156
159
WriteScriptTag ( writer , bodyWriter => Environment . GetInitJavaScript ( bodyWriter , clientOnly ) ) ;
157
- }
158
- finally
159
- {
160
- Environment . ReturnEngineToPool ( ) ;
161
- }
162
- } ) ;
160
+ } ) ;
161
+ }
162
+ finally
163
+ {
164
+ Environment . ReturnEngineToPool ( ) ;
165
+ }
166
+ }
167
+
168
+ private static IHtmlString RenderToString ( Action < StringWriter > withWriter )
169
+ {
170
+ var stringWriter = _sharedStringWriter ;
171
+ if ( stringWriter != null )
172
+ {
173
+ stringWriter . GetStringBuilder ( ) . Clear ( ) ;
174
+ }
175
+ else
176
+ {
177
+ _sharedStringWriter = stringWriter = new StringWriter ( new StringBuilder ( 128 ) ) ;
178
+ }
179
+
180
+ withWriter ( stringWriter ) ;
181
+ return new HtmlString ( stringWriter . ToString ( ) ) ;
163
182
}
164
183
165
184
private static void WriteScriptTag ( TextWriter writer , Action < TextWriter > bodyWriter )
0 commit comments