Exemple #1
0
        public static string ToHtml(Controller controller, string viewToRender, object model = null, bool isPartial = false)
        {
            ViewEngineResult result = null;

            result = GetViewResult(controller.ControllerContext, viewToRender, isPartial);
            if (model == null)
            {
                throw new FileNotFoundException("model cannot be null.");
            }

            StringWriter output;

            using (output = new StringWriter())
            {
                var viewData = controller.ViewData;
                if (model != null)
                {
                    viewData.Model = model;
                }
                var viewContext = new ViewContext(controller.ControllerContext, result.View, viewData, controller.TempData, output, new HtmlHelperOptions());
                result.View.RenderAsync(viewContext);
            }

            return(output.ToString());
        }
        protected string RenderPartialViewToString(string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
            {
                viewName = ControllerContext.ActionDescriptor.DisplayName;
            }
            var pngBinaryData = System.IO.File.ReadAllBytes($"{_env.WebRootPath}/images/background01.jpg");
            var ImgDataURI    = @"data:image/png;base64," + Convert.ToBase64String(pngBinaryData);

            ViewData.Model            = model;
            ViewData["MainImagePath"] = ImgDataURI;
            using (StringWriter sw = new StringWriter())
            {
                var engine = _serviceProvider.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine; // Resolver.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine;
                ViewEngineResult viewResult = engine.FindView(ControllerContext, viewName, false);

                ViewContext viewContext = new ViewContext(
                    ControllerContext,
                    viewResult.View,
                    ViewData,
                    TempData,
                    sw,
                    new HtmlHelperOptions() //Added this parameter in
                    );

                //Everything is async now!
                var t = viewResult.View.RenderAsync(viewContext);
                t.Wait();

                return(sw.GetStringBuilder().ToString());
            }
        }