4

This method works great if I pass null to the last param masterName, My views setup in my class derivved from RazorViewEngine work and all is good. Out of curiosity what is the masterName parameter used for? I first thought maybe it was for a layout.cshtml, however; when I pass it a layout it throws an exception.... Any ideas on how this is supposed to be used, what is it looking for?

Custom View Engine (Hardly LOL)

public class CustomRazorViewEngine : RazorViewEngine
{
    private readonly string[] NewViewFormats = new[]
                                                   {
                                                       "~/Views/Messaging/{0}.cshtml"
                                                   };

    public CustomRazorViewEngine()
    {
        base.ViewLocationFormats = base.ViewLocationFormats.Union(NewViewFormats).ToArray();
    }
}

public string RenderViewToString(string viewName, object model, ControllerContext controllerContext,
                                     string masterName)
    {
        if (string.IsNullOrEmpty(viewName))

            viewName = controllerContext.RouteData.GetRequiredString("action");
        controllerContext.Controller.ViewData.Model = model;
        using (var stringWriter = new StringWriter())
        {
            ViewEngineResult viewEngineResult = ViewEngines.Engines.FindView(controllerContext, viewName, masterName);
            var viewContext = new ViewContext(controllerContext, viewEngineResult.View,
                                              controllerContext.Controller.ViewData,
                                              controllerContext.Controller.TempData,
                                              stringWriter);
            viewEngineResult.View.Render(viewContext, stringWriter);
            return stringWriter.GetStringBuilder().ToString();
        }
    }
6
  • What exception is it throwing, because according to msdn FindView only throws an exception when either controllerContext is null or viewName is null or empty.
    – Major Byte
    Sep 9, 2011 at 16:57
  • ArgumentNullException. If the masterName parameter is anything but null, the search locations in my CustomView Engine, which I might add is hardly custom, are being ignored and it cant find the view. Sep 9, 2011 at 17:05
  • I updated the Code above to show my ViewEngine Class. Sep 9, 2011 at 17:08
  • It would seem that maybe there is a seperate condition that needs to be handled when passing a layout in the ViewEngine class... Sep 9, 2011 at 17:09
  • Well I'll be dang, that is exactly what the problem was. I will post a complete answer. Sep 9, 2011 at 17:30

1 Answer 1

4

So after some more debugging I have found what appears to be the correct answer. First let me state that the masterName parameter is the name of the "Layout" so to say that the view being rendered will use. The catch here is that layout must be able to be located. So instead of the code for the ViewEngine in my original post the following code works as desired.

public string RenderViewToString(string viewName, object model, ControllerContext controllerContext,
                                     string masterName)
    {
        if (string.IsNullOrEmpty(viewName))

            viewName = controllerContext.RouteData.GetRequiredString("action");
        controllerContext.Controller.ViewData.Model = model;
        using (var stringWriter = new StringWriter())
        {
            ViewEngineResult viewEngineResult = ViewEngines.Engines.FindView(controllerContext, viewName, masterName);

            var viewContext = new ViewContext(controllerContext, viewEngineResult.View,
                                              controllerContext.Controller.ViewData,
                                              controllerContext.Controller.TempData,
                                              stringWriter);
            viewEngineResult.View.Render(viewContext, stringWriter);
            return stringWriter.GetStringBuilder().ToString();
        }
    }

public class CustomRazorViewEngine : RazorViewEngine
{
    private readonly string[] NewMasterViewFormats = new[]
                                                         {
                                                             "~/Views/Messaging/Layouts/{0}.cshtml"
                                                         };

    private readonly string[] NewViewFormats = new[]
                                                   {
                                                       "~/Views/Messaging/{0}.cshtml"
                                                   };

    public CustomRazorViewEngine()
    {
        base.ViewLocationFormats = base.ViewLocationFormats.Union(NewViewFormats).ToArray();
        base.MasterLocationFormats = base.MasterLocationFormats.Union(NewMasterViewFormats).ToArray();
    }
}

Now when calling

string returnViewToString = _viewUtils.RenderViewToString("RegistrationEmail", new RegistrationEmailModel
                                                                               {                                                                                      UserName = userName
                                                                               },
                                                     this.ControllerContext,"_RegistrationEmailLayout");

Everything is happy and my layout for the passed in view, if it exists in the folder gets used. This was the highlight of my day... LOL

3
  • Out of curiosity, what is CustomRazorViewEngine for?
    – Andreas
    Jun 29, 2012 at 8:03
  • @Andreas: I think it's pretty apparent that he created custom class to search for views in non-default locations. Jul 9, 2012 at 8:34
  • Can you not simply do return View(viewEngineResult.View);? (I'm not criticizing but trying to learn.)
    – ErikE
    Mar 14, 2014 at 22:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.