66using System . Web ;
77using ServiceStack . Common ;
88using ServiceStack . Common . Web ;
9- using ServiceStack . DataAnnotations ;
109using ServiceStack . Html ;
1110using ServiceStack . ServiceHost ;
1211using ServiceStack . Text ;
13- using ServiceStack . WebHost . Endpoints ;
1412using ServiceStack . WebHost . Endpoints . Extensions ;
1513using ServiceStack . WebHost . Endpoints . Support ;
1614
1715namespace ServiceStack . Razor . Managers
1816{
17+ public delegate string RenderPartialDelegate ( string pageName , object model , bool renderHtml , StreamWriter writer = null , HtmlHelper htmlHelper = null , IHttpRequest httpReq = null ) ;
18+
1919 /// <summary>
2020 /// A common hook into ServiceStack and the hosting infrastructure used to resolve requests.
2121 /// </summary>
@@ -25,24 +25,19 @@ public class PageResolver : EndpointHandlerBase, IViewEngine
2525
2626 private static readonly UTF8Encoding UTF8EncodingWithoutBom = new UTF8Encoding ( encoderShouldEmitUTF8Identifier : false ) ;
2727
28- private readonly IAppHost appHost ;
2928 private readonly IRazorConfig config ;
3029 private readonly ViewManager viewManager ;
30+ public RenderPartialDelegate RenderPartialFn { get ; set ; }
3131
32- public PageResolver ( IAppHost appHost , IRazorConfig config , ViewManager viewManager )
32+ public PageResolver ( IRazorConfig config , ViewManager viewManager )
3333 {
3434 this . RequestName = "Razor_PageResolver" ;
3535
36- this . appHost = appHost ;
37-
3836 this . config = config ;
3937 this . viewManager = viewManager ;
40-
41- this . appHost . CatchAllHandlers . Add ( OnCatchAll ) ;
42- this . appHost . ViewEngines . Add ( this ) ;
4338 }
4439
45- private IHttpHandler OnCatchAll ( string httpmethod , string pathInfo , string filepath )
40+ public IHttpHandler CatchAllHandler ( string httpmethod , string pathInfo , string filepath )
4641 {
4742 //does not have a .cshtml extension
4843 var ext = Path . GetExtension ( pathInfo ) ;
@@ -63,7 +58,7 @@ private IHttpHandler OnCatchAll(string httpmethod, string pathInfo, string filep
6358 ? pathInfo . Substring ( 0 , pathInfo . Length - config . DefaultPageName . Length )
6459 : pathInfo . WithoutExtension ( ) ;
6560
66- var webHostUrl = appHost . Config . WebHostUrl ;
61+ var webHostUrl = config . WebHostUrl ;
6762 return new RedirectHttpHandler
6863 {
6964 AbsoluteUrl = webHostUrl . IsNullOrEmpty ( )
@@ -109,29 +104,29 @@ public virtual bool ProcessRequest(IHttpRequest httpReq, IHttpResponse httpRes,
109104 return true ;
110105 }
111106
112- public void ResolveAndExecuteRazorPage ( IHttpRequest httpReq , IHttpResponse httpRes , object dto , RazorPage razorView = null )
107+ public void ResolveAndExecuteRazorPage ( IHttpRequest httpReq , IHttpResponse httpRes , object dto , RazorPage razorPage = null )
113108 {
114109 var viewName = httpReq . GetItem ( "View" ) as string ;
115- if ( razorView == null && viewName != null )
110+ if ( razorPage == null && viewName != null )
116111 {
117- razorView = this . viewManager . GetRazorViewByName ( viewName ) ;
112+ razorPage = this . viewManager . GetRazorViewByName ( viewName ) ;
118113 }
119114 else
120115 {
121- razorView = razorView
116+ razorPage = razorPage
122117 ?? this . viewManager . GetRazorViewByName ( httpReq . OperationName ) //Request DTO
123118 ?? this . viewManager . GetRazorView ( httpReq , dto ) ; // Response DTO
124119 }
125120
126- if ( razorView == null )
121+ if ( razorPage == null )
127122 {
128123 httpRes . StatusCode = ( int ) HttpStatusCode . NotFound ;
129124 return ;
130125 }
131126
132127 using ( var writer = new StreamWriter ( httpRes . OutputStream , UTF8EncodingWithoutBom ) )
133128 {
134- var page = CreateRazorPageInstance ( httpReq , httpRes , dto , razorView ) ;
129+ var page = CreateRazorPageInstance ( httpReq , httpRes , dto , razorPage ) ;
135130
136131 var includeLayout = ! ( httpReq . GetParam ( "format" ) ?? "" ) . Contains ( "bare" ) ;
137132 if ( includeLayout )
@@ -147,17 +142,19 @@ public void ResolveAndExecuteRazorPage(IHttpRequest httpReq, IHttpResponse httpR
147142 ?? DefaultLayoutName ;
148143
149144 var layoutView = this . viewManager . GetRazorViewByName ( layout , httpReq , dto ) ;
150- var layoutPage = CreateRazorPageInstance ( httpReq , httpRes , dto , layoutView ) ;
151-
152- var childBody = ms . ToArray ( ) . FromUtf8Bytes ( ) ;
153- layoutPage . SetChildPage ( page , childBody ) ;
154- layoutPage . WriteTo ( writer ) ;
145+ if ( layoutView != null )
146+ {
147+ var layoutPage = CreateRazorPageInstance ( httpReq , httpRes , dto , layoutView ) ;
148+
149+ var childBody = ms . ToArray ( ) . FromUtf8Bytes ( ) ;
150+ layoutPage . SetChildPage ( page , childBody ) ;
151+ layoutPage . WriteTo ( writer ) ;
152+ return ;
153+ }
155154 }
156155 }
157- else
158- {
159- page . WriteTo ( writer ) ;
160- }
156+
157+ page . WriteTo ( writer ) ;
161158 }
162159 }
163160
@@ -173,9 +170,9 @@ public void EnsureCompiled(RazorPage page, IHttpResponse response)
173170 page . IsValid = true ;
174171 }
175172
176- private IRazorViewPage CreateRazorPageInstance ( IHttpRequest request , IHttpResponse response , object dto , RazorPage razorPage )
173+ private IRazorViewPage CreateRazorPageInstance ( IHttpRequest httpReq , IHttpResponse httpRes , object dto , RazorPage razorPage )
177174 {
178- EnsureCompiled ( razorPage , response ) ;
175+ EnsureCompiled ( razorPage , httpRes ) ;
179176
180177 //don't proceed any further, the background compiler found there was a problem compiling the page, so throw instead
181178 if ( razorPage . CompileException != null )
@@ -186,10 +183,10 @@ private IRazorViewPage CreateRazorPageInstance(IHttpRequest request, IHttpRespon
186183 //else, EnsureCompiled() ensures we have a page type to work with so, create an instance of the page
187184 var page = ( IRazorViewPage ) razorPage . ActivateInstance ( ) ;
188185
189- page . Init ( viewEngine : this , httpReq : request , httpRes : response ) ;
186+ page . Init ( viewEngine : this , httpReq : httpReq , httpRes : httpRes ) ;
190187
191188 //deserialize the model.
192- PrepareAndSetModel ( page , request , dto ) ;
189+ PrepareAndSetModel ( page , httpReq , dto ) ;
193190
194191 return page ;
195192 }
@@ -200,7 +197,7 @@ private void PrepareAndSetModel(IRazorViewPage page, IHttpRequest httpReq, objec
200197 if ( hasModel == null ) return ;
201198
202199 if ( hasModel . ModelType == typeof ( DynamicRequestObject ) )
203- dto = new DynamicRequestObject ( httpReq ) ;
200+ dto = new DynamicRequestObject ( httpReq , dto ) ;
204201
205202 var model = dto ?? DeserializeHttpRequest ( hasModel . ModelType , httpReq , httpReq . ContentType ) ;
206203
@@ -234,17 +231,17 @@ public virtual string RenderPartial(string pageName, object model, bool renderHt
234231 }
235232 else
236233 {
237- foreach ( var viewEngine in appHost . ViewEngines )
234+ if ( RenderPartialFn != null )
235+ {
236+ RenderPartialFn ( pageName , model , renderHtml , writer , htmlHelper , httpReq ) ;
237+ }
238+ else
238239 {
239- if ( viewEngine == this || ! viewEngine . HasView ( pageName , httpReq ) ) continue ;
240- viewEngine . RenderPartial ( pageName , model , renderHtml , writer , htmlHelper ) ;
241- return null ;
240+ writer . Write ( "<!--No RenderPartialFn, skipping {0}-->" . Fmt ( pageName ) ) ;
242241 }
243- writer . Write ( "<!--{0} not found-->" . Fmt ( pageName ) ) ;
244242 }
245243 return null ;
246244 }
247-
248245 }
249246
250247}
0 commit comments