Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ASP.NET Core MVC Render View to String
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
public interface IViewRendererService
{
Task<string> RenderViewToStringAsync(string viewPath, object model);
Task<string> RenderViewToStringAsync(object model);
}
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Routing;
public class ViewRendererService : IViewRendererService
{
private readonly IRazorViewEngine _razorViewEngine;
private readonly ITempDataProvider _tempDataProvider;
private readonly IActionContextAccessor _actionContextAccessor;
public ViewRendererService(IRazorViewEngine razorViewEngine,
ITempDataProvider tempDataProvider,
IActionContextAccessor actionContextAccessor)
{
_razorViewEngine = razorViewEngine;
_tempDataProvider = tempDataProvider;
_actionContextAccessor = actionContextAccessor;
}
public async Task<string> RenderViewToStringAsync(object model)
{
var actionName = _actionContextAccessor.ActionContext.ActionDescriptor.RouteValues["action"];
return await RenderViewToStringAsync(actionName, model);
}
public async Task<string> RenderViewToStringAsync(string viewPath, object model)
{
var actionContext = _actionContextAccessor.ActionContext;
var viewEngineResult = _razorViewEngine.FindView(actionContext, viewPath, false);
if (viewEngineResult.View == null || (!viewEngineResult.Success))
{
throw new ArgumentNullException($"Unable to find view '{viewPath}'");
}
var view = viewEngineResult.View;
var viewDictionary = new ViewDataDictionary(new EmptyModelMetadataProvider(), actionContext.ModelState);
viewDictionary.Model = model;
var tempData = new TempDataDictionary(actionContext.HttpContext,
_tempDataProvider);
using (var sw = new StringWriter())
{
var viewContext = new ViewContext(actionContext, view, viewDictionary, tempData, sw, new HtmlHelperOptions());
await view.RenderAsync(viewContext);
return sw.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment