@@ -135,42 +135,54 @@ public IRazorView ResolveAndExecuteRazorPage(IHttpRequest httpReq, IHttpResponse
135
135
httpRes . StatusCode = ( int ) HttpStatusCode . NotFound ;
136
136
return null ;
137
137
}
138
-
138
+
139
139
using ( var writer = new StreamWriter ( httpRes . OutputStream , UTF8EncodingWithoutBom ) )
140
140
{
141
141
var page = CreateRazorPageInstance ( httpReq , httpRes , model , razorPage ) ;
142
142
143
143
var includeLayout = ! ( httpReq . GetParam ( QueryStringFormatKey ) ?? "" ) . Contains ( NoTemplateFormatValue ) ;
144
144
if ( includeLayout )
145
145
{
146
- using ( var ms = new MemoryStream ( ) )
147
- using ( var childWriter = new StreamWriter ( ms , UTF8EncodingWithoutBom ) )
146
+ var result = ExecuteRazorPageWithLayout ( httpReq , httpRes , model , page , ( ) =>
148
147
{
149
- //child page needs to execute before master template to populate ViewBags, sections, etc
150
- page . WriteTo ( childWriter ) ;
148
+ return httpReq . GetItem ( LayoutKey ) as string
149
+ ?? page . Layout
150
+ ?? DefaultLayoutName ;
151
+ } ) ;
152
+
153
+ writer . Write ( result . Item2 ) ;
154
+ return result . Item1 ;
155
+ }
151
156
152
- var layout = httpReq . GetItem ( LayoutKey ) as string
153
- ?? page . Layout
154
- ?? DefaultLayoutName ;
157
+ page . WriteTo ( writer ) ;
158
+ return page ;
159
+ }
160
+ }
161
+
162
+ private Tuple < IRazorView , string > ExecuteRazorPageWithLayout ( IHttpRequest httpReq , IHttpResponse httpRes , object model , IRazorView page , Func < string > layout )
163
+ {
164
+ using ( var ms = new MemoryStream ( ) )
165
+ {
166
+ using ( var childWriter = new StreamWriter ( ms , UTF8EncodingWithoutBom ) )
167
+ {
168
+ //child page needs to execute before master template to populate ViewBags, sections, etc
169
+ page . WriteTo ( childWriter ) ;
170
+ var childBody = ms . ToArray ( ) . FromUtf8Bytes ( ) ;
155
171
156
- var childBody = ms . ToArray ( ) . FromUtf8Bytes ( ) ;
157
- var layoutPage = this . viewManager . GetPageByName ( layout , httpReq , model ) ;
172
+ var layoutName = layout ( ) ;
173
+ if ( ! String . IsNullOrEmpty ( layoutName ) )
174
+ {
175
+ var layoutPage = this . viewManager . GetPageByName ( layoutName , httpReq , model ) ;
158
176
if ( layoutPage != null )
159
177
{
160
178
var layoutView = CreateRazorPageInstance ( httpReq , httpRes , model , layoutPage ) ;
161
-
162
179
layoutView . SetChildPage ( page , childBody ) ;
163
- layoutView . WriteTo ( writer ) ;
164
- return layoutView ;
180
+ return ExecuteRazorPageWithLayout ( httpReq , httpRes , model , layoutView , ( ) => layoutView . Layout ) ;
165
181
}
166
-
167
- writer . Write ( childBody ) ;
168
- return page ;
169
182
}
170
- }
171
183
172
- page . WriteTo ( writer ) ;
173
- return page ;
184
+ return Tuple . Create ( page , childBody ) ;
185
+ }
174
186
}
175
187
}
176
188
0 commit comments