1212
1313#if LEGACYASPNET
1414using System . Web ;
15- using System . Web . Mvc ;
1615using IHtmlHelper = System . Web . Mvc . HtmlHelper ;
1716#else
18- using System . Text . Encodings . Web ;
1917using Microsoft . AspNetCore . Mvc . Rendering ;
2018using IHtmlString = Microsoft . AspNetCore . Html . IHtmlContent ;
21- using Microsoft . AspNetCore . Html ;
2219#endif
2320
2421#if LEGACYASPNET
@@ -32,7 +29,6 @@ namespace React.AspNet
3229 /// </summary>
3330 public static class HtmlHelperExtensions
3431 {
35-
3632 /// <summary>
3733 /// Gets the React environment
3834 /// </summary>
@@ -70,24 +66,28 @@ public static IHtmlString React<T>(
7066 Action < Exception , string , string > exceptionHandler = null
7167 )
7268 {
73- try
69+ return new ActionHtmlString ( writer =>
7470 {
75- var reactComponent = Environment . CreateComponent ( componentName , props , containerId , clientOnly , serverOnly ) ;
76- if ( ! string . IsNullOrEmpty ( htmlTag ) )
71+ try
7772 {
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 ) ) ;
7985 }
80- if ( ! string . IsNullOrEmpty ( containerClass ) )
86+ finally
8187 {
82- reactComponent . ContainerClass = containerClass ;
88+ Environment . ReturnEngineToPool ( ) ;
8389 }
84- var result = reactComponent . RenderHtml ( clientOnly , serverOnly , exceptionHandler ) ;
85- return new HtmlString ( result ) ;
86- }
87- finally
88- {
89- Environment . ReturnEngineToPool ( ) ;
90- }
90+ } ) ;
9191 }
9292
9393 /// <summary>
@@ -116,25 +116,30 @@ public static IHtmlString ReactWithInit<T>(
116116 Action < Exception , string , string > exceptionHandler = null
117117 )
118118 {
119- try
119+ return new ActionHtmlString ( writer =>
120120 {
121- var reactComponent = Environment . CreateComponent ( componentName , props , containerId , clientOnly ) ;
122- if ( ! string . IsNullOrEmpty ( htmlTag ) )
121+ try
123122 {
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 ( ) ) ) ;
125137 }
126- if ( ! string . IsNullOrEmpty ( containerClass ) )
138+ finally
127139 {
128- reactComponent . ContainerClass = containerClass ;
140+ Environment . ReturnEngineToPool ( ) ;
129141 }
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+ } ) ;
138143 }
139144
140145 /// <summary>
@@ -144,55 +149,34 @@ public static IHtmlString ReactWithInit<T>(
144149 /// <returns>JavaScript for all components</returns>
145150 public static IHtmlString ReactInitJavaScript ( this IHtmlHelper htmlHelper , bool clientOnly = false )
146151 {
147- try
148- {
149- return GetScriptTag ( Environment . GetInitJavaScript ( clientOnly ) ) ;
150- }
151- finally
152+ return new ActionHtmlString ( writer =>
152153 {
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+ } ) ;
155163 }
156164
157- private static IHtmlString GetScriptTag ( string script )
165+ private static void WriteScriptTag ( TextWriter writer , Action < TextWriter > bodyWriter )
158166 {
159- #if LEGACYASPNET
160- var tag = new TagBuilder ( "script" )
161- {
162- InnerHtml = script ,
163- } ;
164-
167+ writer . Write ( "<script" ) ;
165168 if ( Environment . Configuration . ScriptNonceProvider != null )
166169 {
167- tag . Attributes . Add ( "nonce" , Environment . Configuration . ScriptNonceProvider ( ) ) ;
170+ writer . Write ( " nonce=\" " ) ;
171+ writer . Write ( Environment . Configuration . ScriptNonceProvider ( ) ) ;
172+ writer . Write ( "\" " ) ;
168173 }
169174
170- return new HtmlString ( tag . ToString ( ) ) ;
171- #else
172- var tag = new TagBuilder ( "script" ) ;
173- tag . InnerHtml . AppendHtml ( script ) ;
175+ writer . Write ( ">" ) ;
174176
175- if ( Environment . Configuration . ScriptNonceProvider != null )
176- {
177- tag . Attributes . Add ( "nonce" , Environment . Configuration . ScriptNonceProvider ( ) ) ;
178- }
177+ bodyWriter ( writer ) ;
179178
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>" ) ;
196180 }
197181 }
198182}
0 commit comments