App.Web and App.Views are my projects in one solution, I put my views in App.Views and precompiled with RazorGenerator. It's working well if I used App.Web like,
~/Views/Index.cshtmlis virtual path of my view in App.View
It can successfully render this view in App.Web
public ActionResult Index() {
return View("~/Views/Index.cshtml");
}
But when I try to RenderViewToString, it returns null.
class FakeController : ControllerBase
{
protected override void ExecuteCore() { }
public static string RenderViewToString(string controllerName, string viewName, object viewData)
{
using (var writer = new StringWriter())
{
var routeData = new RouteData();
routeData.Values.Add("controller", controllerName);
var fakeControllerContext = new ControllerContext(new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://google.com", null), new HttpResponse(null))), routeData, new FakeController());
var razorViewEngine = new RazorViewEngine();
var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewName, "", false);
var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(viewData), new TempDataDictionary(), writer);
razorViewResult.View.Render(viewContext, writer);
return writer.ToString();
}
}
}
And this is how can all it,
FakeController.RenderViewToString("FakeName", "~/Views/Index.csthml", MessageModel);
This is discussed and probably solved in asp.net core, but I'm working with asp.net mvc 5.
Could you please help me to figure out, why it's not working?

You are trying to use Razor View Engine for getting precompiled views. This is a mistake. Razor engine produces views based on cshtml files. However in case of precompiled views, cshtml files have been already compiled by
RazorGeneratorto a set of classes derivied fromSystem.Web.Mvc.WebViewPage. These classes override methodExecute()(autogenerated byRazorGeneratorbased on input cshtml) that write html to outputTextWriter. Original view files (cshtml) are not required anymore and thus are not deployed with the application. When you call Razor that tries to locate cshtml and build view based on it, it expectedly returnsnullview.ASP.NET MVC supports multiple view engines (the classes that implement
System.Web.Mvc.IViewEngine).RazorViewEngineis one of them.RazorGenerator.MvcNuGet package adds its own view engine (PrecompiledMvcEngine) that works based on precompiled views. Registered view engines are stored inViewEngines.Enginescollection. When you installRazorGenerator.MvcNuGet package, it addsRazorGeneratorMvcStartclass that registers instance ofPrecompiledMvcEngine:You should use this instance of
PrecompiledMvcEngineinstead ofRazorViewEnginefor accessing precompiled views. Here is adjusted code ofRenderViewToStringmethod:One important note: you should install
RazorGenerator.MvcNuGet package into project with your views (App.Views), not into Web application project, becausePrecompiledMvcEnginetakes current assembly as the source for the precompiled views. Aslo make sure thatRazorGeneratorMvcStartwas not added toApp.Webproject. It happened for me when I have added reference toRazorGenerator.Mvc.dllassembly.Sample Project on GitHub