I have found a few other questions like this, however every single one has not been able to fix this problem I am having. I continuously get the same problem.
I have checked out this and that and multiple others! Every single thing I get the same problem.
I get the following exception:
The view at '~/Views/Email/Ticket.cshtml' must derive from WebViewPage, or WebViewPage<TModel>.
The code I am using is:
public static string RenderViewToString(string controllerName, string viewName, object viewModel)
{
HttpContextWrapper context = new HttpContextWrapper(HttpContext.Current);
RouteData routeData = new System.Web.Routing.RouteData();
routeData.Values.Add("controller", controllerName);
ControllerContext controllerContext = new ControllerContext(context, routeData, new RenderController());
RazorViewEngine razorViewEngine = new RazorViewEngine();
ViewEngineResult razorViewResult = razorViewEngine.FindView(controllerContext, viewName, string.Empty, false);
using (StringWriter writer = new StringWriter())
{
ViewDataDictionary viewData = new ViewDataDictionary(viewModel);
var viewContext = new ViewContext(controllerContext, razorViewResult.View, viewData, new TempDataDictionary(), writer);
razorViewResult.View.Render(viewContext, writer);
writer.Flush();
return writer.GetStringBuilder().ToString();
}
}
The RenderController is just:
private class RenderController : ControllerBase
{
protected override void ExecuteCore()
{
}
}
No matter what I have done, or different methods I have tried nothing seems to make this error go away, I have tried making the view inherit the objects but you can't have @model and @inherit in the same thing. Getting quite aggravated over this as I have followed pretty much everyone's instructions on other questions/posts but hasn't been able to help me.