I am trying to use the code below to render a Razor view template into a string:

using (var sw = new StringWriter())
            {
                var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName);
                //var viewContext = new ViewContext(controllerContext, viewResult.View, vd, new TempDataDictionary(), sw);
                var viewContext = new ViewContext(new ControllerContext(), viewResult.View, vd, new TempDataDictionary(), sw);
                viewResult.View.Render(viewContext, sw);
                html = sw.GetStringBuilder().ToString();
            }

But since this will be invoked from a Web Api controller, how can I imitate and pass a valid MVC controller context for this to work?

Related posts

Recent Viewed