Service for rendering Razor MVC views to strings. This can be used for sending emails, generating PDFs, and any number of things.
You must also install either GravySoft.Razor.StringGenerator.AspNetCore or GravySoft.Razor.StringGenerator.NetFramework.
- Inject an
IRazorViewToStringRenderer
to your class.
private IRazorViewToStringRenderer renderer;
public ExampleClass(IRazorViewToStringRenderer renderer) => this.renderer = renderer;
- Execute your call.
string text = await renderer.RenderViewToStringAsync("~/Views/Email.cshtml", new EmailModel
{
FirstName = "John",
LastName = "Doe",
FavoriteColors = new List<string> { "Red", "Purple", "Orange" }
});
Make sure you use the full path of the view
The service needs to be registered in as scoped so that it has access to a request HttpContext.
In Startup.ConfigureServices
services.AddScoped<IRazorViewToStringRenderer, RazorViewToStringRenderer>();
In your NinjectModule
Bind<RazorViewToStringRenderer>().To<IRazorViewToStringRenderer>();
1.0.0
- Initial Release