In previous versions of ASP.NET it was possible, although not very simple, to render Razor views as strings. The methods I've seem are to use a fake controller, or also to use some external engine like RazorEngine.
Now, many things are changed with ASP.NET 5 and I was wondering whether or not this is now simpler than before. So in the new version of the framework is there one straightforward way to render Razor views as strings or we still need to use the methods from the previous versions?
I use the following types injected from the
IServiceProvider
:I render the content using the following method:
I call it like this:
Of course, my
viewDataDictionary
andactionContext
variables are set by another method for encapsulation. A modification to thenew ViewDataDictionary
line can result in a typed Model being bound to your View if you choose.This code uses heavy usings, I think I've listed them below. Otherwise, VS2015 is pretty good about finding them.
This was written under beta-3; it still builds, but some things may change. I'll try to come back here to update if it does.
There is a solution that I've used an year ago. I'm not sure if there is a new/better way to do, but it solves the problem.