Last active
August 1, 2023 14:35
-
-
Save zckkte/66b04a18519284ebe25e83391cc9913b to your computer and use it in GitHub Desktop.
ASP.NET Core Render Razor ViewModel to String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
How can I use it?
@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
Thanks for sharing...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How To use it