Skip to content

HttpContext extensions to allow Razor view (.cshtml) rendering to a string

License

Notifications You must be signed in to change notification settings

wessleym/WessleyMitchell.Web.DotNetCore.ViewRenderer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

WessleyMitchell.Web.DotNetCore.ViewRenderer

HttpContext extensions to allow Razor view (.cshtml) rendering to a string

I wanted to contribute this to ASP.NET Core: dotnet/aspnetcore#37187

But Microsoft decided it would be better as a separate library. This is that library.

Installation

Add this NuGet package:

    PM> Install-Package WessleyMitchell.Web.DotNetCore.ViewRenderer

Usage

Add a using statement:

using WessleyMitchell.Web.DotNetCore.ViewRenderer;

Invoke the renderer:

string html = await HttpContext.RenderViewAsync("~/Pages/SomePage.cshtml", new SomePageModel("X"), isMainPage: false);

Known Issues

I have not been able to get this code to work for a .cshtml with @page at the top. It only seems to work with partials (no @page). If @page is used, Model is null on the .cshtml page. Again, this does not occur when no @page is at the top. If someone knows a solution, I would appreciate a pull request. Thank you.

About

HttpContext extensions to allow Razor view (.cshtml) rendering to a string

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages