View to String #ASP_NET
/// <summary> Отображает контент представления для визуализации чек листа в задачах </summary>
/// <param name="WorkflowInstance_Id"></param>
/// <returns></returns>
public string ViewHistory(long WorkflowInstance_Id)
{
var controllerContext = CreateController<CheckHistoryController>().ControllerContext;
controllerContext.Controller.ViewData.Model = WorkflowInstance_Id;
string Content = "";
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(controllerContext, "/Modules/Sfmnt.Elma.ListTask.Web/Views/CheckHistory/ViewHistory.cshtml");
var viewContext = new ViewContext(controllerContext, viewResult.View, controllerContext.Controller.ViewData , controllerContext.Controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(controllerContext, viewResult.View);
Content = sw.GetStringBuilder().ToString();
}
return Content;
}
/// <summary>
/// Creates an instance of an MVC controller from scratch
/// when no existing ControllerContext is present
/// </summary>
/// <typeparam name="T">Type of the controller to create</typeparam>
/// <returns></returns>
private T CreateController<T>(RouteData routeData = null) where T : Controller, new()
{
T controller = new T();
HttpContextBase wrapper = null;
if (HttpContext.Current != null)
wrapper = new HttpContextWrapper(System.Web.HttpContext.Current);
if (routeData == null)
routeData = new RouteData();
if (!routeData.Values.ContainsKey("controller") && !routeData.Values.ContainsKey("Controller"))
routeData.Values.Add("controller", controller.GetType().Name
.ToLower()
.Replace("controller", ""));
controller.ControllerContext = new ControllerContext(wrapper, routeData, controller);
return controller;
}