27

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();
    }
}
Vadim Ovchinnikov
  • 13,327
  • 5
  • 62
  • 90
Martijn
  • 24,441
  • 60
  • 174
  • 261

5 Answers5

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();
}
DavidG
  • 113,891
  • 12
  • 217
  • 223
  • 1
    Thanks. How did you find out? I have the feeling that you digged in to this. And it works by the way, thanks again :) – Martijn Aug 09 '15 at 17:40
  • 1
    Yeah, I had a dig around. Took a while but I thought it would be useful for myself anyway. – DavidG Aug 09 '15 at 20:37
  • 1
    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. – Martijn Aug 10 '15 at 07:13
  • 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! – DavidG Aug 10 '15 at 11:54
  • Just a note that in core 2 the `ICompositeViewEngine` no longer contains `FindPartialView` :( so many breaking changes... – mmix Dec 15 '17 at 10:42
  • 1
    @mmix Guess I'll have to look at updating this for .Net Core 2. I don't even think this works well for 1! – DavidG Dec 15 '17 at 11:07
  • @mmix Didn't take long to fix it up :) – DavidG Dec 15 '17 at 11:48
  • @DavidG Is it possible to generalize this method (outside the controller action) to get html string by just passing the .cshtml view and model and the same html string which can be sent as email body or other many application scenarios. – Rahul Uttarkar Mar 19 '19 at 11:30
  • @DavidG Is there a meaninful way to do this without having to inject ICompositeViewEngine? I have this as a protected method inside a BaseController from which all my other controllers inherit so that I can access it from any controller. I hate the idea of having to modify constructors of every controller in my solution just so that I can inject ICompositeViewEngine. This DI everything obsession has gone way too far. – Marko Nov 28 '19 at 05:55
  • @RahulUttakar if you want to render Razor outside the context of any Controller then you'll have to create the context yourself -- which this library does so I'd recommend using this: https://github.com/soundaranbu/RazorTemplating – CajunCoding May 23 '22 at 18:43
  • @Marko Yes you don't have to use Constructor injection, you can get the Service from the Service Provider in your base class which is now part of `HttpContext`: `this.Controller.HttpContext..RequestServices.GetRequiredService();` – CajunCoding May 23 '22 at 18:46
  • FYI to anyone looking at this solution, the `ICompositeViewEngine.FindView()` method will only search for a View in the default `../Views/...` folder construct. If your views exist outside of that folder in a custom folder then you can use `ICompositeViewEngine.GetView(...)` which takes in base path and relative view path. I offer my implementation which searches both (also available on Nuget): https://github.com/cajuncoding/PdfTemplating.XslFO/blob/9f8476c510d8717e37449739382b95ced328e530/PdfTemplating.XslFO.Razor.AspNetCoreMvc/MvcRazorViewRenderer.cs#L131 – CajunCoding May 23 '22 at 18:50
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;
}
silkfire
  • 24,585
  • 15
  • 82
  • 105
Martin Tomes
  • 181
  • 1
  • 10
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;
}
Richard Mneyan
  • 656
  • 1
  • 13
  • 20
2

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);
Proggear
  • 662
  • 6
  • 10
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);
  • 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): https://github.com/cajuncoding/PdfTemplating.XslFO/blob/9f8476c510d8717e37449739382b95ced328e530/PdfTemplating.XslFO.Razor.AspNetCoreMvc/MvcRazorViewRenderer.cs#L131 – CajunCoding May 23 '22 at 18:53
  • 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! – Brennan Pope May 19 '23 at 14:45