CompareCloudware.Web/Controllers/HomeController - Copy.cs
/// <summary>Renders a view to string.</summary>
Renders a view to string.
Renders a view to string .
public static string RenderViewToString(this Controller controller,
                                                string viewName, object viewData)
        {
            var sb = new StringBuilder();
            var memWriter = new StringWriter(sb);
            string filename = "PrintResult3";
            string url = ConfigurationManager.AppSettings["PDFViewPath"];
            var httpRequest = new HttpRequest(filename, url, null);
            var fakeResponse = new HttpResponse(memWriter);
            var fakeContext = new HttpContext(httpRequest, fakeResponse);
            var fakeControllerContext = new ControllerContext(
                new HttpContextWrapper(fakeContext),
                controller.ControllerContext.RouteData,
                controller.ControllerContext.Controller);
            var oldContext = HttpContext.Current;
            HttpContext.Current = fakeContext;
            var html = new HtmlHelper(new ViewContext(fakeControllerContext, new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), memWriter),
                new ViewPage());
            System.Web.Mvc.Html.RenderPartialExtensions.RenderPartial(html, viewName, viewData);
            HttpContext.Current = oldContext;
            memWriter.Flush();
            return sb.ToString();
        }
public static string RenderViewToString ( this Controller controller , string viewName , object viewData ) { var sb = new StringBuilder ( ) ; var memWriter = new StringWriter ( sb ) ; string filename = " " ; string url = ConfigurationManager . AppSettings [ " " ] ; var httpRequest = new HttpRequest ( filename , url , null ) ; var fakeResponse = new HttpResponse ( memWriter ) ; var fakeContext = new HttpContext ( httpRequest , fakeResponse ) ; var fakeControllerContext = new ControllerContext ( new HttpContextWrapper ( fakeContext ) , controller . ControllerContext . RouteData , controller . ControllerContext . Controller ) ; var oldContext = HttpContext . Current ; HttpContext . Current = fakeContext ; var html = new HtmlHelper ( new ViewContext ( fakeControllerContext , new FakeView ( ) , new ViewDataDictionary ( ) , new TempDataDictionary ( ) , memWriter ) , new ViewPage ( ) ) ; System . Web . Mvc . Html . RenderPartialExtensions . RenderPartial ( html , viewName , viewData ) ; HttpContext . Current = oldContext ; memWriter . Flush ( ) ; return sb . ToString ( ) ; }
Renders a view to string.
Renders a view to string .
//Create memory writer //Create fake http context to render the view //var fakeContext = new HttpContext(HttpContext.Current.Request, fakeResponse); //Use HtmlHelper to render partial view to fake context //html.RenderPartial(viewName, viewData); //Restore context //Flush memory and return output
Controller
this
viewName
string
viewData
object