Example #1
0
 /// <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());
         }
 }
Example #2
0
 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());
     }
 }
Example #4
0
 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;
 }
Example #5
0
        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()));
        }
Example #6
0
 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());
     }
 }
Example #7
0
        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());
        }
Example #8
0
        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());
        }
Example #9
0
        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);
        }
Example #10
0
        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);
        }
Example #11
0
        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));
            }
        }
Example #12
0
        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));
                }
            }
        }
Example #13
0
 public void SetChildPage(IRazorView childPage, string childBody)
 {
     this.ChildPage = childPage;
     this.ChildBody = childBody;
 }
Example #14
0
        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);
        }
Example #15
0
        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);
                }
            }
        }
Example #16
0
        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);
            }
        }