public string GetRazorViewAsString(string filePath, object model = null)
{
var resultString = new StringWriter();
var context = new HttpContextWrapper(HttpContext.Current);
var routeData = new RouteData();
// Creatign the controller context
var controllerContext = new ControllerContext(new RequestContext(context, routeData), new DummyController());
// Rebdering the view and getting the html to resultString
var razor = new RazorView(controllerContext, filePath, null, false, null);
razor.Render(new ViewContext(controllerContext, razor, new ViewDataDictionary(model), new TempDataDictionary(), resultString), resultString);
// Returning the generated html
return resultString.ToString();
}
public class DummyController : Controller { }
Currently, we are using above code for generating HTML for a view. In that, view path is a virtual path.
Now, we are planning to move the views outside of the project. So keeping virtual path is not possible now.
Is there any way of creating HTML by taking full path of the view