30

I've created a new MVC6 project and building a new site. The goal is to get the rendered result of a view. I found the following code, but I can't get it to work because I can't find the ControllerContext and the ViewEngines.

Here is the code I want to rewrite:

protected string RenderPartialViewToString(string viewName, object model) { if (string.IsNullOrEmpty(viewName)) viewName = ControllerContext.RouteData.GetRequiredString("action"); ViewData.Model = model; using (StringWriter sw = new StringWriter()) { ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); viewResult.View.Render(viewContext, sw); return sw.GetStringBuilder().ToString(); } } 
3
  • is this code inside of a controller? ViewEngines and ControllerContext are in System.Web.Mvc.. ControllerBase also has a property of ControllerContext Commented Aug 9, 2015 at 15:13
  • @JamieD77 This is for MVC6 which doesn't have these. Commented Aug 9, 2015 at 15:22
  • nevermind.. need to label my sample projects better :) Commented Aug 10, 2015 at 1:20

5 Answers 5

41

Update: I'm updating this to work with .Net Core 2.x as the APIs have changed since 2015!

First of all we can leverage the built in dependency injection that comes with ASP.Net MVC Core which will give us the ICompositeViewEngine object we need to render our views manually. So for example, a controller would look like this:

public class MyController : Controller { private ICompositeViewEngine _viewEngine; public MyController(ICompositeViewEngine viewEngine) { _viewEngine = viewEngine; } //Rest of the controller code here } 

Next, the code we actually need to render a view. Note that is is now an async method as we will be making asynchronous calls internally:

private async Task<string> RenderPartialViewToString(string viewName, object model) { if (string.IsNullOrEmpty(viewName)) viewName = ControllerContext.ActionDescriptor.ActionName; ViewData.Model = model; using (var writer = new StringWriter()) { ViewEngineResult viewResult = _viewEngine.FindView(ControllerContext, viewName, false); ViewContext viewContext = new ViewContext( ControllerContext, viewResult.View, ViewData, TempData, writer, new HtmlHelperOptions() ); await viewResult.View.RenderAsync(viewContext); return writer.GetStringBuilder().ToString(); } } 

And to call the method, it's as simple as this:

public async Task<IActionResult> Index() { var model = new TestModel { SomeProperty = "whatever" } var renderedView = await RenderPartialViewToString("NameOfView", model); //Do what you want with the renderedView here return View(); } 
Sign up to request clarification or add additional context in comments.

12 Comments

Thanks. How did you find out? I have the feeling that you digged in to this. And it works by the way, thanks again :)
Yeah, I had a dig around. Took a while but I thought it would be useful for myself anyway.
May I ask where you digged around? I've tried to find it myself offcourse, but I couldn't find much on Google on this topic.
I played around with the sample app for a while, then had a look at the source code on GitHub. Then when it occurred to me that DI was such a huge part of MVC6, I took a look at what items had been put into the list of available services and found the composite view engine!
@mmix Guess I'll have to look at updating this for .Net Core 2. I don't even think this works well for 1!
|
16

The released dotnet core 1.0 has changed, this version of the above code works with 1.0 RTM.

protected string RenderPartialViewToString(string viewName, object model) { if (string.IsNullOrEmpty(viewName)) viewName = ControllerContext.ActionDescriptor.DisplayName; ViewData.Model = model; 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(); } } 

These usings are required for this code to compile:

using System.IO; using Microsoft.AspNetCore.Mvc.ViewEngines; using Microsoft.AspNetCore.Mvc.ViewFeatures; 

I also had to add a DI interfaces to the controller constructor:

IServiceProvider serviceProvider 

My account constructor looks like this now:

public AccountController( UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, IEmailSender emailSender, ISmsSender smsSender, ILoggerFactory loggerFactory, IServiceProvider serviceProvider) { _userManager = userManager; _signInManager = signInManager; _emailSender = emailSender; _smsSender = smsSender; _logger = loggerFactory.CreateLogger<AccountController>(); _serviceProvider = serviceProvider; } 

2 Comments

I am getting "The name 'ViewData' does not exist in the current context" with the provided references. Same for TempData.
Also, what would be a necessary change to make this into a general service using dependency injection rather than a method for a single controller?
3

Solution by Martin Tomes works well. My changes: removed serviceProvider and get ICompositeViewEngine in constructor via DI. Constructor looks like:

private readonly ICompositeViewEngine _viewEngine; public AccountController( UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager, IEmailSender emailSender, ISmsSender smsSender, ILoggerFactory loggerFactory, ICompositeViewEngine viewEngine) { _userManager = userManager; _signInManager = signInManager; _emailSender = emailSender; _smsSender = smsSender; _logger = loggerFactory.CreateLogger<AccountController>(); _viewEngine = viewEngine;; } 

and put

ViewEngineResult viewResult = _viewEngine.FindView(ControllerContext, viewName, false); 

instead of

var engine = _serviceProvider.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine; // Resolver.GetService(typeof(ICompositeViewEngine)) as ICompositeViewEngine; ViewEngineResult viewResult = engine.FindView(ControllerContext, viewName, false); 

Comments

2

The solution provided by Martin Tomes worked for me but I had to replace:

ViewEngineResult viewResult = engine.FindView(ControllerContext, viewName, false); 

with

ViewEngineResult viewResult = engine.GetView(_env.WebRootPath, viewName, false); 

Also in controller constructor had to add

private IHostingEnvironment _env; public AccountController(IHostingEnvironment env) { _env = env; } 

Comments

2

The solution provided by DavidG worked fine but I change it to a independent service.

namespace WebApplication.Services { public interface IViewRenderService { Task<string> RenderPartialViewToString(Controller Controller, string viewName, object model); } public class ViewRenderService: IViewRenderService { private readonly ICompositeViewEngine _viewEngine; public ViewRenderService(ICompositeViewEngine viewEngine) { _viewEngine = viewEngine; } public async Task<string> RenderPartialViewToString(Controller Controller, string viewName, object model) { if (string.IsNullOrEmpty(viewName)) viewName = Controller.ControllerContext.ActionDescriptor.ActionName; Controller.ViewData.Model = model; using (var writer = new StringWriter()) { ViewEngineResult viewResult = _viewEngine.FindView(Controller.ControllerContext, viewName, false); ViewContext viewContext = new ViewContext( Controller.ControllerContext, viewResult.View, Controller.ViewData, Controller.TempData, writer, new HtmlHelperOptions() ); await viewResult.View.RenderAsync(viewContext); return writer.GetStringBuilder().ToString(); } } } } 

Add service to startup:

services.AddScoped<IViewRenderService, ViewRenderService>(); 

Now use dependency injection like this:

public class MyController : Controller { private readonly IViewRenderService _viewRender; public MyController(IViewRenderService viewRender) { _viewRender = viewRender; } //Rest of the controller code here } 

Now you can use it in action methode like this:

var renderedView = await_viewRender.RenderPartialViewToString(this,"nameofview", model); 

2 Comments

This is a nice version that can be injected. To anyone interested, I offer my implementation which searches both default ../Views/... folders but also any custom folder for the View (also available on Nuget): github.com/cajuncoding/PdfTemplating.XslFO/blob/…
I was upgrading a similar view rendering service targeting .net framework and was trying to decide if I wanted to swtch from using a ControllerContext to a Controller. Thank you for saving me some time!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.