MVC 5 Render View to String

后端 未结 5 1004
梦谈多话
梦谈多话 2020-12-13 09:44

It seems, most code for rendering view into string doesn\'t work in MVC 5.

I have latest MVC 5.1.2 templates and I am trying to render view into string.



        
5条回答
  •  误落风尘
    2020-12-13 10:46

    I had an immediate need to return 6 partial views as strings in a JSON object. Instead of creating a static method, and passing all the unneeded parameters, I decided to add protected methods to our ControllerBase class that derives from Controller, and is used as the base class for all of our controllers.

    Here is a fully functional ControllerBase class that provides this functionality, and works very similar to the PartialView() and View() methods that are in the Controller class. It includes the additions from @Alok.

    public abstract class ControllerBase : Controller
    {
        #region PartialViewToString
    
        protected string PartialViewToString(string partialViewName, object model = null)
        {
            ControllerContext controllerContext = new ControllerContext(Request.RequestContext, this);
    
            return ViewToString(
                controllerContext,
                ViewEngines.Engines.FindPartialView(controllerContext, partialViewName) ?? throw new FileNotFoundException("Partial view cannot be found."),
                model
            );
        }
    
        #endregion
    
        #region ViewToString
    
        protected string ViewToString(string viewName, object model = null)
        {
            ControllerContext controllerContext = new ControllerContext(Request.RequestContext, this);
    
            return ViewToString(
                controllerContext,
                ViewEngines.Engines.FindView(controllerContext, viewName, null) ?? throw new FileNotFoundException("View cannot be found."),
                model
            );
        }
    
        protected string ViewToString(string viewName, string controllerName, string areaName, object model = null)
        {
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", controllerName);
    
            if (areaName != null)
            {
                routeData.Values.Add("Area", areaName);
                routeData.DataTokens["area"] = areaName;
            }
    
            ControllerContext controllerContext = new ControllerContext(HttpContext, routeData, this);
    
            return ViewToString(
                controllerContext,
                ViewEngines.Engines.FindView(controllerContext, viewName, null) ?? throw new FileNotFoundException("View cannot be found."),
                model
            );
        }
    
        #endregion
    
        #region Private Methods
    
        private string ViewToString(ControllerContext controllerContext, ViewEngineResult viewEngineResult, object model)
        {
            using (StringWriter writer = new StringWriter())
            {
                ViewContext viewContext = new ViewContext(
                    controllerContext,
                    viewEngineResult.View,
                    new ViewDataDictionary(model),
                    new TempDataDictionary(),
                    writer
                );
    
                viewEngineResult.View.Render(viewContext, writer);
    
                return writer.ToString();
            }
        }
    
        #endregion
    }
    

提交回复
热议问题