void ITemplate.Run(ExecuteContext context, TextWriter textWriter) { var builder = new StringBuilder(); _executeContextAdapter = new ExecuteContextAdapter(this, context); using (var writer = new StringWriter(builder)) { _executeContextAdapter.CurrentWriter = writer; OnStart(); Execute(); OnEnd(); _executeContextAdapter.CurrentWriter = null; } var parent = ResolveLayout(Layout); if (parent == null && string.IsNullOrEmpty(Layout)) { var result = builder.ToString(); textWriter.Write(result); return; } if (parent == null) { throw new InvalidOperationException("Layout template was not found."); } parent.SetData(null, ViewBag); var exposingParent = parent as ExposingTemplate; if (exposingParent == null) { throw new InvalidOperationException("Unexpected layout template base type."); } exposingParent._templateVisitor = _templateVisitor; var bodyWriter = new TemplateWriter(tw => tw.Write(builder.ToString())); _executeContextAdapter.PushBody(bodyWriter); _executeContextAdapter.PushSections(); parent.Run(_executeContextAdapter.Context, textWriter); }
string ITemplate.Run(ExecuteContext context) { var builder = new StringBuilder(); _executeContextAdapter = new ExecuteContextAdapter(this, context); using (var writer = new StringWriter(builder)) { _executeContextAdapter.CurrentWriter = writer; OnStart(); Execute(); OnEnd(); _executeContextAdapter.CurrentWriter = null; } var parent = ResolveLayout(Layout); if (parent == null && string.IsNullOrEmpty(Layout)) { return(builder.ToString()); } if (parent == null) { throw new InvalidOperationException("Layout template was not found."); } var exposingParent = parent as ExposingTemplate; if (exposingParent == null) { throw new InvalidOperationException("Unexpected layout template base type."); } exposingParent._templateVisitor = _templateVisitor; var bodyWriter = new TemplateWriter(tw => tw.Write(builder.ToString())); _executeContextAdapter.PushBody(bodyWriter); return(parent.Run(_executeContextAdapter.Context)); }