12
12
13
13
#if LEGACYASPNET
14
14
using System . Web ;
15
- using System . Web . Mvc ;
16
15
using IHtmlHelper = System . Web . Mvc . HtmlHelper ;
17
16
#else
18
- using System . Text . Encodings . Web ;
19
17
using Microsoft . AspNetCore . Mvc . Rendering ;
20
18
using IHtmlString = Microsoft . AspNetCore . Html . IHtmlContent ;
21
- using Microsoft . AspNetCore . Html ;
22
19
#endif
23
20
24
21
#if LEGACYASPNET
@@ -32,7 +29,6 @@ namespace React.AspNet
32
29
/// </summary>
33
30
public static class HtmlHelperExtensions
34
31
{
35
-
36
32
/// <summary>
37
33
/// Gets the React environment
38
34
/// </summary>
@@ -70,24 +66,28 @@ public static IHtmlString React<T>(
70
66
Action < Exception , string , string > exceptionHandler = null
71
67
)
72
68
{
73
- try
69
+ return new ActionHtmlString ( writer =>
74
70
{
75
- var reactComponent = Environment . CreateComponent ( componentName , props , containerId , clientOnly , serverOnly ) ;
76
- if ( ! string . IsNullOrEmpty ( htmlTag ) )
71
+ try
77
72
{
78
- reactComponent . ContainerTag = htmlTag ;
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
+ writer . Write ( reactComponent . RenderHtml ( clientOnly , serverOnly , exceptionHandler ) ) ;
79
85
}
80
- if ( ! string . IsNullOrEmpty ( containerClass ) )
86
+ finally
81
87
{
82
- reactComponent . ContainerClass = containerClass ;
88
+ Environment . ReturnEngineToPool ( ) ;
83
89
}
84
- var result = reactComponent . RenderHtml ( clientOnly , serverOnly , exceptionHandler ) ;
85
- return new HtmlString ( result ) ;
86
- }
87
- finally
88
- {
89
- Environment . ReturnEngineToPool ( ) ;
90
- }
90
+ } ) ;
91
91
}
92
92
93
93
/// <summary>
@@ -116,25 +116,30 @@ public static IHtmlString ReactWithInit<T>(
116
116
Action < Exception , string , string > exceptionHandler = null
117
117
)
118
118
{
119
- try
119
+ return new ActionHtmlString ( writer =>
120
120
{
121
- var reactComponent = Environment . CreateComponent ( componentName , props , containerId , clientOnly ) ;
122
- if ( ! string . IsNullOrEmpty ( htmlTag ) )
121
+ try
123
122
{
124
- reactComponent . ContainerTag = htmlTag ;
123
+ var reactComponent = Environment . CreateComponent ( componentName , props , containerId , clientOnly ) ;
124
+ if ( ! string . IsNullOrEmpty ( htmlTag ) )
125
+ {
126
+ reactComponent . ContainerTag = htmlTag ;
127
+ }
128
+
129
+ if ( ! string . IsNullOrEmpty ( containerClass ) )
130
+ {
131
+ reactComponent . ContainerClass = containerClass ;
132
+ }
133
+
134
+ writer . Write ( reactComponent . RenderHtml ( clientOnly , exceptionHandler : exceptionHandler ) ) ;
135
+ writer . WriteLine ( ) ;
136
+ WriteScriptTag ( writer , bodyWriter => bodyWriter . Write ( reactComponent . RenderJavaScript ( ) ) ) ;
125
137
}
126
- if ( ! string . IsNullOrEmpty ( containerClass ) )
138
+ finally
127
139
{
128
- reactComponent . ContainerClass = containerClass ;
140
+ Environment . ReturnEngineToPool ( ) ;
129
141
}
130
- var html = reactComponent . RenderHtml ( clientOnly , exceptionHandler : exceptionHandler ) ;
131
-
132
- return new HtmlString ( html + System . Environment . NewLine + RenderToString ( GetScriptTag ( reactComponent . RenderJavaScript ( ) ) ) ) ;
133
- }
134
- finally
135
- {
136
- Environment . ReturnEngineToPool ( ) ;
137
- }
142
+ } ) ;
138
143
}
139
144
140
145
/// <summary>
@@ -144,55 +149,34 @@ public static IHtmlString ReactWithInit<T>(
144
149
/// <returns>JavaScript for all components</returns>
145
150
public static IHtmlString ReactInitJavaScript ( this IHtmlHelper htmlHelper , bool clientOnly = false )
146
151
{
147
- try
148
- {
149
- return GetScriptTag ( Environment . GetInitJavaScript ( clientOnly ) ) ;
150
- }
151
- finally
152
+ return new ActionHtmlString ( writer =>
152
153
{
153
- Environment . ReturnEngineToPool ( ) ;
154
- }
154
+ try
155
+ {
156
+ WriteScriptTag ( writer , bodyWriter => bodyWriter . Write ( Environment . GetInitJavaScript ( clientOnly ) ) ) ;
157
+ }
158
+ finally
159
+ {
160
+ Environment . ReturnEngineToPool ( ) ;
161
+ }
162
+ } ) ;
155
163
}
156
164
157
- private static IHtmlString GetScriptTag ( string script )
165
+ private static void WriteScriptTag ( TextWriter writer , Action < TextWriter > bodyWriter )
158
166
{
159
- #if LEGACYASPNET
160
- var tag = new TagBuilder ( "script" )
161
- {
162
- InnerHtml = script ,
163
- } ;
164
-
167
+ writer . Write ( "<script" ) ;
165
168
if ( Environment . Configuration . ScriptNonceProvider != null )
166
169
{
167
- tag . Attributes . Add ( "nonce" , Environment . Configuration . ScriptNonceProvider ( ) ) ;
170
+ writer . Write ( " nonce=\" " ) ;
171
+ writer . Write ( Environment . Configuration . ScriptNonceProvider ( ) ) ;
172
+ writer . Write ( "\" " ) ;
168
173
}
169
174
170
- return new HtmlString ( tag . ToString ( ) ) ;
171
- #else
172
- var tag = new TagBuilder ( "script" ) ;
173
- tag . InnerHtml . AppendHtml ( script ) ;
175
+ writer . Write ( ">" ) ;
174
176
175
- if ( Environment . Configuration . ScriptNonceProvider != null )
176
- {
177
- tag . Attributes . Add ( "nonce" , Environment . Configuration . ScriptNonceProvider ( ) ) ;
178
- }
177
+ bodyWriter ( writer ) ;
179
178
180
- return tag ;
181
- #endif
182
- }
183
-
184
- // In ASP.NET Core, you can no longer call `.ToString` on `IHtmlString`
185
- private static string RenderToString ( IHtmlString source )
186
- {
187
- #if LEGACYASPNET
188
- return source . ToString ( ) ;
189
- #else
190
- using ( var writer = new StringWriter ( ) )
191
- {
192
- source . WriteTo ( writer , HtmlEncoder . Default ) ;
193
- return writer . ToString ( ) ;
194
- }
195
- #endif
179
+ writer . Write ( "</script>" ) ;
196
180
}
197
181
}
198
182
}
0 commit comments