locked
Need a workaround to use MVC2 view page or control to generate html without controller context RRS feed

  • Question

  • User-1956403626 posted

    Hi,

    We are working in MVC 2 and cannot mirgrate to newer versions.

    I need a workaround to use MVC2 view page or user control to generate html for email without using ControllerContext, which means it should be a standalone template engine which can be called from service layers or webservice or any application where i can create the engine and generate html to send emails.

    I cannot use nVelocity, spark, stringtemplate or nHaml thus we are constrained to use MVC view page or user controls.

    If you guys know a way to create controller context(similar like mocking(not Moq) it or stubbing it to render partial views to string please help me out.

    If it had been MVC3 we would had used Razor but we are working in MVC2 and cannot migrate to newer versions.

    thanks

    Thursday, January 19, 2012 7:08 AM

Answers

All replies

  • User1682618242 posted

    Use ths controller extension:

    using System.IO;
    using System.Web.Mvc;
    
    public static class ControllerHelper
    {
        #region Render Partial to String
        public static string RenderPartialViewToString(this Controller controller)
        {
            return RenderPartialViewToString(null, null);
        }
    
        public static string RenderPartialViewToString(this Controller controller, string viewName)
        {
            return RenderPartialViewToString(controller, viewName, null);
        }
    
        public static string RenderPartialViewToString(this Controller controller, object model)
        {
            return RenderPartialViewToString(controller, null, model);
        }
    
        public static string RenderPartialViewToString(this Controller controller, string viewName, object model)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = controller.ControllerContext.RouteData.GetRequiredString("action");
    
            controller.ViewData.Model = model;
    
            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                viewResult.View.Render(viewContext, sw);
    
                return sw.GetStringBuilder().ToString();
            }
        }
        #endregion
    
        #region Render View to String
        public static string RenderViewToString(this Controller controller)
        {
            return RenderViewToString(null, null);
        }
    
        public static string RenderViewToString(this Controller controller, string viewName)
        {
            return RenderViewToString(controller, viewName, null);
        }
    
        public static string RenderViewToString(this Controller controller, object model)
        {
            return RenderViewToString(controller, null, model);
        }
    
        public static string RenderViewToString(this Controller controller, string viewName, object model)
        {
            return RenderViewToString(controller, viewName, model, string.Empty);
        }
    
        public static string RenderViewToString(this Controller controller, string viewName, object model, string masterName)
        {
            if (string.IsNullOrEmpty(viewName))
                viewName = controller.ControllerContext.RouteData.GetRequiredString("action");
    
            controller.ViewData.Model = model;
    
            using (StringWriter sw = new StringWriter())
            {
                ViewEngineResult viewResult = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, masterName);
                ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, sw);
                viewResult.View.Render(viewContext, sw);
    
                return sw.GetStringBuilder().ToString();
            }
        }
        #endregion
    }

    And you call it like this (from an Action):

    var html = this.RenderViewToString("ViewToRenderAsHtmlString", model);

    and make sure that any CSS stylings are inline. The you just set it to a Mail message body.

    Thursday, January 19, 2012 7:34 AM
  • User-1956403626 posted

    thanks for the code .. but my engine has to run outside of MVC

    Thursday, January 19, 2012 10:20 PM
  • User-1956403626 posted

    thanks again but we are tied up to framework 3.5. is it possible to use razor in 3.5??

    Friday, January 20, 2012 3:45 AM
  • User-683735340 posted

    NO Razor is only for 4.0

    Friday, January 20, 2012 4:56 AM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, January 23, 2012 9:04 AM
  • User-1956403626 posted

    http://www.codewrecks.com/blog/index.php/2009/04/05/could-not-load-type-systemwebmvcviewpage/

    this has to be there to work ViewPage<T>

    Monday, January 23, 2012 9:42 AM