Exemplo n.º 1
0
        public string RenderViewToString(string viewName, object model, string masterName)
        {
            if (string.IsNullOrEmpty(viewName))
            {
                viewName = ControllerContext.RouteData.GetRequiredString("action");
            }

            ViewBag.BaseUrl = MvcApplication.ConvertToAbsoluteHref(Url.Action("Index", "Home"));
            ViewData.Model  = model;

            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult  = ViewEngines.Engines.FindView(ControllerContext, viewName, masterName);
                ViewContext      viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
                viewResult.View.Render(viewContext, sw);

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