I have looked for similar questions but have not managed to build a solution yet.
Basically what I need is to render views from strings in memory.
Example:
public IActionResult Test()
{
var model = new MyModel
{
Name = "Joe"
};
var cshtml = "<h1> Hello @Model.Name </h1>";
var myView = MyAwesomeEngine.CreateView(cshtml, model);
return View(myView)
//or
return myView
}
Is this possible? I have tried RazorViewEngine inheritance without success, what would be the correct way to render views from string without looking for files in view paths?
I am using aspnetcore 1.1
As Legends suggested using IFileProvider method worked correctly!
Using the example of this link I was able to do it (without the database)
This was my test IFileInfo implementation:
public class MemoryFileInfo : IFileInfo
{
private byte[] _testContent = Encoding.ASCII.GetBytes(@"@model TestModel
<p>@Model.Name</p>");
private string _viewName;
public bool Exists { get
{
return _viewName.Equals("/Views/Home/TestView.cshtml");
}
}
public MemoryFileInfo(string name)
{
_viewName = name;
}
public long Length => _testContent.Length;
public string PhysicalPath => null;
public string Name => _viewName;
public DateTimeOffset LastModified => DateTimeOffset.Now;
public bool IsDirectory => false;
public Stream CreateReadStream()
{
return new MemoryStream(_testContent);
}
}
This is another great solution RazorEngine but is has no support for netcoreapp1.1 yet