/// <summary>An IRazorView extension method that renders the section to HTML.</summary> /// /// <param name="razorView"> The razorView to act on.</param> /// <param name="sectionName">Name of the section.</param> /// /// <returns>A string.</returns> public static string RenderSectionToHtml(this IRazorView razorView, string sectionName) { using (var ms = new MemoryStream()) using (var writer = new StreamWriter(ms)) { razorView.RenderSection(sectionName, writer); writer.Flush(); return(ms.ToArray().FromUtf8Bytes()); } }
public void Init(IViewEngine viewEngine, IHttpRequest httpReq, IHttpResponse httpRes, IRazorView razorPage, Dictionary <string, object> scopeArgs = null, ViewDataDictionary viewData = null) { ViewEngine = viewEngine; HttpRequest = httpReq; HttpResponse = httpRes; RazorPage = razorPage; //ScopeArgs = scopeArgs; this.viewData = viewData; }
public static string RenderSectionToHtml(this IRazorView razorView, string sectionName) { using (var ms = MemoryStreamFactory.GetStream()) { var writer = new StreamWriter(ms); razorView.RenderChildSection(sectionName, writer); writer.Flush(); return(ms.ReadToEnd()); } }
public void Init(IViewEngine viewEngine, IRequest httpReq, IResponse httpRes, IRazorView razorPage, Dictionary<string, object> scopeArgs = null, ViewDataDictionary viewData = null) { ViewEngine = viewEngine; HttpRequest = httpReq as IHttpRequest; HttpResponse = httpRes as IHttpResponse; RazorPage = razorPage; //ScopeArgs = scopeArgs; this.viewData = viewData; }
private static ActionContext GetActionContext(IDictionary <string, object> routeValues, IRazorView razorView = null) { var httpContext = new DefaultHttpContext(); var serviceProvider = new Mock <IServiceProvider>(); serviceProvider.Setup(p => p.GetService(typeof(IRazorView))) .Returns(razorView ?? Mock.Of <IRazorView>()); httpContext.RequestServices = serviceProvider.Object; var routeData = new RouteData { Values = routeValues }; return(new ActionContext(httpContext, routeData, new ActionDescriptor())); }
public string RenderView(YOYO.Owin.IOwinContext httpContext, string viewName, object model, DynamicDictionary viewbag) { using (var context = new RenderTemplateContext() { TemplateName = viewName, Path = httpContext.Request.Path, Model = model, ModelType = model?.GetType(), ViewBag = viewbag }) { IRazorView view = templateService.GetTemplate(context); view.Render(); this.RenderContext = context; return(context.ToString()); } }
public string RenderToHtml(RazorPage razorPage, out IRazorView razorView, object model = null, string layout = null) { if (razorPage == null) { throw new ArgumentNullException("razorPage"); } var httpReq = new BasicRequest(); if (layout != null) { httpReq.Items[RazorPageResolver.LayoutKey] = layout; } razorView = PageResolver.ExecuteRazorPage(httpReq, httpReq.Response, model, razorPage); var ms = (MemoryStream)httpReq.Response.OutputStream; return(ms.ToArray().FromUtf8Bytes()); }
public string RenderToHtml(RazorPage razorPage, out IRazorView razorView, object model = null, string layout = null) { if (razorPage == null) { throw new ArgumentNullException(nameof(razorPage)); } var httpReq = new BasicRequest(); if (layout != null) { httpReq.Items[Keywords.Template] = layout; } var ms = (MemoryStream)httpReq.Response.OutputStream; razorView = PageResolver.ExecuteRazorPage(httpReq, ms, model, razorPage); return(ms.ReadToEnd()); }
private void PrepareAndSetModel(IRazorView page, IRequest httpReq, object dto) { if (!(page is IHasModel hasModel)) { return; } if (hasModel.ModelType == typeof(DynamicRequestObject)) { dto = new DynamicRequestObject(httpReq, dto); } var model = dto ?? DeserializeHttpRequestAsync(hasModel.ModelType, httpReq, httpReq.ContentType).GetResult(); if (model.GetType().IsAnonymousType()) { model = new DynamicRequestObject(httpReq, model); } hasModel.SetModel(model); }
private void PrepareAndSetModel(IRazorView page, IHttpRequest httpReq, object dto) { var hasModel = page as IHasModel; if (hasModel == null) { return; } if (hasModel.ModelType == typeof(DynamicRequestObject)) { dto = new DynamicRequestObject(httpReq, dto); } var model = dto ?? DeserializeHttpRequest(hasModel.ModelType, httpReq, httpReq.ContentType); if (model.GetType().IsAnonymousType()) { model = new DynamicRequestObject(httpReq, model); } hasModel.SetModel(model); }
private Tuple <IRazorView, string> ExecuteRazorPageWithLayout(RazorPage razorPage, IRequest httpReq, IResponse httpRes, object model, IRazorView pageInstance, Func <string> layout) { using (var ms = MemoryStreamFactory.GetStream()) { var childWriter = new StreamWriter(ms, UTF8EncodingWithoutBom); //ms disposed in using //child page needs to execute before master template to populate ViewBags, sections, etc try { pageInstance.WriteTo(childWriter); } catch (StopExecutionException) { } if (httpRes.IsClosed) { return(null); } var childBody = ms.ReadToEnd(); var layoutName = layout(); if (!string.IsNullOrEmpty(layoutName)) { var layoutPage = viewManager.GetLayoutPage(layoutName, razorPage, httpReq, model); if (layoutPage != null) { var layoutView = CreateRazorPageInstance(httpReq, httpRes, model, layoutPage); layoutView.SetChildPage(pageInstance, childBody); return(ExecuteRazorPageWithLayout(layoutPage, httpReq, httpRes, model, layoutView, () => layoutView.Layout)); } } return(Tuple.Create(pageInstance, childBody)); } }
private Tuple <IRazorView, string> ExecuteRazorPageWithLayout(IHttpRequest httpReq, IHttpResponse httpRes, object model, IRazorView page, Func <string> layout) { using (var ms = new MemoryStream()) { using (var childWriter = new StreamWriter(ms, UTF8EncodingWithoutBom)) { //child page needs to execute before master template to populate ViewBags, sections, etc page.WriteTo(childWriter); var childBody = ms.ToArray().FromUtf8Bytes(); var layoutName = layout(); if (!String.IsNullOrEmpty(layoutName)) { var layoutPage = this.viewManager.GetPageByName(layoutName, httpReq, model); if (layoutPage != null) { var layoutView = CreateRazorPageInstance(httpReq, httpRes, model, layoutPage); layoutView.SetChildPage(page, childBody); return(ExecuteRazorPageWithLayout(httpReq, httpRes, model, layoutView, () => layoutView.Layout)); } } return(Tuple.Create(page, childBody)); } } }
public void SetChildPage(IRazorView childPage, string childBody) { this.ChildPage = childPage; this.ChildBody = childBody; }
private void PrepareAndSetModel(IRazorView page, IHttpRequest httpReq, object dto) { var hasModel = page as IHasModel; if (hasModel == null) return; if (hasModel.ModelType == typeof(DynamicRequestObject)) dto = new DynamicRequestObject(httpReq, dto); var model = dto ?? DeserializeHttpRequest(hasModel.ModelType, httpReq, httpReq.ContentType); if (model.GetType().IsAnonymousType()) { model = new DynamicRequestObject(httpReq, model); } hasModel.SetModel(model); }
private Tuple<IRazorView, string> ExecuteRazorPageWithLayout(IHttpRequest httpReq, IHttpResponse httpRes, object model, IRazorView page, Func<string> layout) { using (var ms = new MemoryStream()) { using (var childWriter = new StreamWriter(ms, UTF8EncodingWithoutBom)) { //child page needs to execute before master template to populate ViewBags, sections, etc page.WriteTo(childWriter); var childBody = ms.ToArray().FromUtf8Bytes(); var layoutName = layout(); if (!String.IsNullOrEmpty(layoutName)) { var layoutPage = this.viewManager.GetPageByName(layoutName, httpReq, model); if (layoutPage != null) { var layoutView = CreateRazorPageInstance(httpReq, httpRes, model, layoutPage); layoutView.SetChildPage(page, childBody); return ExecuteRazorPageWithLayout(httpReq, httpRes, model, layoutView, () => layoutView.Layout); } } return Tuple.Create(page, childBody); } } }
public string RenderToHtml(RazorPage razorPage, out IRazorView razorView, object model = null, string layout = null) { if (razorPage == null) throw new ArgumentNullException("razorPage"); var httpReq = new BasicRequest(); if (layout != null) { httpReq.Items[RazorPageResolver.LayoutKey] = layout; } razorView = PageResolver.ExecuteRazorPage(httpReq, httpReq.Response, model, razorPage); var ms = (MemoryStream)httpReq.Response.OutputStream; return ms.ToArray().FromUtf8Bytes(); }
private Tuple<IRazorView, string> ExecuteRazorPageWithLayout(RazorPage razorPage, IRequest httpReq, IResponse httpRes, object model, IRazorView pageInstance, Func<string> layout) { using (var ms = MemoryStreamFactory.GetStream()) { var childWriter = new StreamWriter(ms, UTF8EncodingWithoutBom); //ms disposed in using //child page needs to execute before master template to populate ViewBags, sections, etc try { pageInstance.WriteTo(childWriter); } catch (StopExecutionException) { } if (httpRes.IsClosed) return null; var childBody = ms.ToArray().FromUtf8Bytes(); var layoutName = layout(); if (!string.IsNullOrEmpty(layoutName)) { var layoutPage = viewManager.GetLayoutPage(layoutName, razorPage, httpReq, model); if (layoutPage != null) { var layoutView = CreateRazorPageInstance(httpReq, httpRes, model, layoutPage); layoutView.SetChildPage(pageInstance, childBody); return ExecuteRazorPageWithLayout(layoutPage, httpReq, httpRes, model, layoutView, () => layoutView.Layout); } } return Tuple.Create(pageInstance, childBody); } }