I have implemented rendering a Razor view to a string, according to this: https://stackoverflow.com/a/2759898/6237687
But my problem is that it takes some time to render the view, and it blocks the thread, so I want to call it asynchronously.
I found that Asp.Net Core has "RenderAsync()", but it does not seem to be in MVC 5...
So, my question is how can I call the "Render()" method asynchronously, like in Asp.Net Core?
My rendering function:
private string RenderViewToString(string viewPath, object model)
{
// Finds the ViewEngine for this view:
ViewEngineResult viewEngineResult = ViewEngines.Engines.FindView(Context, viewPath, null);
if (viewEngineResult == null)
{
viewEngineResult = ViewEngines.Engines.FindPartialView(Context, viewPath);
if (viewEngineResult == null)
throw new FileNotFoundException("View: \"" + viewPath + "\" cannot be found.");
}
// Gets the view, and attaches the model to the view data:
IView view = viewEngineResult.View;
Context.Controller.ViewData = this.ViewData;
Context.Controller.ViewData.Model = model;
// Renders the view:
string result = null;
using (var sw = new StringWriter())
{
var ctx = new ViewContext(Context, view, Context.Controller.ViewData, Context.Controller.TempData, sw);
view.Render(ctx, sw);
result = sw.ToString();
}
return result;
}
Problem is in: "view.Render(ctx, sw);"