Render.View in MVC 5

Topics: ASP.NET MVC
Sep 10, 2013 at 7:19 AM
Edited Sep 10, 2013 at 7:19 AM
Hey,

I tried to render a razor template manually to a string using the View.Render method:
public string RenderRazorViewToString(string viewName, object model) {
    ViewData.Model = model;
    using (var sw = new StringWriter()) {
        var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
        viewResult.View.Render(viewContext, sw);
        viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
        return sw.GetStringBuilder().ToString();
    }
}
This worked in MVC 4 but does not work in MVC 5. The result looks like this:
<$A$><h1>Hello</h1> 
<table</$A$><$B$> class="table table-striped"</$B$><$C$>> <tbody</$C$><$D$>></tbody>
</table></$D$>
Looks like the View.Render pre-processes the view. What has changed in MVC 5? Is there still a method to process Razor templates?
Sep 10, 2013 at 6:05 PM
Thanks for reporting this. This is a known issue and is fixed in the latest RC release. You can download the latest RC bits from here.

If for some reason you would like to continue to use the Preview bits, you can work around this issue by disabling browser link. For more details on how to disable this you can see here.