hohoholololo
hohoholololo

Reputation: 41

The RouteData must contain an item named 'controller' with a non-empty string value

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.

Upvotes: 3

Views: 2802

Answers (1)

Onur Gelmez
Onur Gelmez

Reputation: 1044

you need to add route data. Exception tell you everything.

var routeData = new RouteData() ;
routeData.Values.Add("Controller", "Home");
routeData.Values.Add("Action", "Index");

Upvotes: 4

Related Questions