Skip to content

Instantly share code, notes, and snippets.

@zckkte
Last active August 1, 2023 14:35
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save zckkte/66b04a18519284ebe25e83391cc9913b to your computer and use it in GitHub Desktop.
Save zckkte/66b04a18519284ebe25e83391cc9913b to your computer and use it in GitHub Desktop.
ASP.NET Core Render Razor ViewModel 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();
}
}
}
@fnfworkshop
Copy link

How To use it

@polr
Copy link

polr commented Apr 13, 2023

How can I use it?

@zckkte
Copy link
Author

zckkte commented Apr 13, 2023

@polr @fnfworkshop Something like the following,

val renderedView = await viewRenderer.RenderViewToStringAsync("path/to/view.cshtml", model)

But, this was written way back. You're better off using Razor.Templating.Core

@prince272
Copy link

Thanks for sharing...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment