MVC 5 Render View to String

后端 未结 5 1005
梦谈多话
梦谈多话 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:23

    following is the solution that works with session and areas on MVC5.

    public class FakeController : ControllerBase
    {
        protected override void ExecuteCore() { }
        public static string RenderViewToString(string controllerName, string viewName,string areaName, object viewData,RequestContext rctx)
        {
            try
            {
                using (var writer = new StringWriter())
                {
                    var routeData = new RouteData();
                    routeData.Values.Add("controller", controllerName);
                    routeData.Values.Add("Area", areaName);
                    routeData.DataTokens["area"] = areaName;
    
                    var fakeControllerContext = new ControllerContext(rctx, new FakeController());
                    //new ControllerContext(new HttpContextWrapper(new HttpContext(new HttpRequest(null, "http://google.com", null), new HttpResponse(null))), routeData, new FakeController());
                    fakeControllerContext.RouteData = routeData;
    
                    var razorViewEngine = new RazorViewEngine();
    
                    var razorViewResult = razorViewEngine.FindView(fakeControllerContext, viewName, "", false);
    
                    var viewContext = new ViewContext(fakeControllerContext, razorViewResult.View, new ViewDataDictionary(viewData), new TempDataDictionary(), writer);
    
                    razorViewResult.View.Render(viewContext, writer);
                    return writer.GetStringBuilder().ToString();
                    //use example
                    //String renderedHTML = RenderViewToString("Email", "MyHTMLView", myModel );
                    //where file MyHTMLView.cstml is stored in Views/Email/MyHTMLView.cshtml. Email is a fake controller name.
                }
            }
            catch (Exception ex)
            {
                //do your exception handling here
            }
        }
    }
    

    here is how you call this from another controller

    var modal = getModal(params);
    return FakeController.RenderViewToString(controllerName, viewName, areaName, modal, this.Request.RequestContext);
    

    using requestcontext we can easily pass current session in fakecontroller and render razor string.

提交回复
热议问题