public static string RazorViewRender(object model, string filePath)
{
var sw = new StringWriter();
var context = new HttpContextWrapper(System.Web.HttpContext.Current);
var routeData = new RouteData() ;
HomeController home= new HomeController();
var controllerContext = new ControllerContext(new RequestContext(context, routeData), home);
var razor = new RazorView(controllerContext, filePath, null, false, null);
razor.Render(new ViewContext(controllerContext, razor, new ViewDataDictionary(model), new TempDataDictionary(), sw), sw);
return sw.ToString();
}
in this code,
razor.render
line program gives error
'The RouteData must contain an item named 'controller' with a non-empty string value.'
i guess it can't find my homecontroller, my project has areas. How can i solve this problem.
you need to add route data. Exception tell you everything.
var routeData = new RouteData() ;
routeData.Values.Add("Controller", "Home");
routeData.Values.Add("Action", "Index");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With