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(); } }
ControllerContext(ControllerContext) Initializes a new instance of the ControllerContext class by using the specified controller context. ControllerContext(HttpContextBase, RouteData, ControllerBase)
Learn more at Overview of ASP.NET Core MVC and Get started with ASP.NET Core MVC and Visual Studio. The controller is a UI-level abstraction. Its responsibilities are to ensure request data is valid and to choose which view (or result for an API) should be returned.
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(); }
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With