Render View as String - c#

Am trying to render a view as a string to be used as an email template. Am currently trying to implement this example:
https://weblog.west-wind.com/posts/2012/May/30/Rendering-ASPNET-MVC-Views-to-String
However am having difficulty with this section of code:
public ViewRenderer(ControllerContext controllerContext = null)
{
// Create a known controller from HttpContext if no context is passed
if (controllerContext == null)
{
if (HttpContext.Current != null)
controllerContext = CreateController<ErrorController>().ControllerContext;
else
throw new InvalidOperationException(
"ViewRenderer must run in the context of an ASP.NET " +
"Application and requires HttpContext.Current to be present.");
}
Context = controllerContext;
}
Visual Studio is giving me the following error:
"The type or namespace name 'ErrorController' could not be found (are you missing a using directive or an assembly reference?)"
Am probably missing something obvious but can't see what it is. Any ideas?

If you need to Render your view as a string, here is an extension method for the controller I wrote.
NB: I will try and find the exact link I used to help me with this, and update my answer when I find it.
Here is another link, describing this method.
This should do the trick:
public static string RenderViewToString(this Controller controller, string viewName, object model)
{
var context = controller.ControllerContext;
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
var viewData = new ViewDataDictionary(model);
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(context, viewName);
var viewContext = new ViewContext(context, viewResult.View, viewData, new TempDataDictionary(), sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
If you want to call this from a controller, you simply do the following:
var strView = this.RenderViewToString("YourViewName", yourModel);

Related

Access ViewContext.ViewBag without editing the .cshtml view file

I am trying to render a view to html string using the RenderViewToString method below. When calling the viewResult.View.Render and passing the viewContext, it results in the ViewBag being null in the .cshtml view. Example code of my renderer:
public string RenderViewToString(ControllerContext context, string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = context.RouteData.GetRequiredString("action");
ViewDataDictionary viewData = new ViewDataDictionary(model);
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindView(context, viewName, null);
ViewContext viewContext = new ViewContext(context, viewResult.View, viewData, new TempDataDictionary(), sw);
var thisViewBagHasValues = ViewBag;
var thisViewBagIsNull = viewContext.ViewBag;
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
I think by calling the #ViewContext.Controller.ViewBag in the .cshtml file, we can then access the ViewBag data. However, is there a way to access the ViewBag in the .cshtml file without editing the view file? Such as setting a value to the ViewContext.ViewBag before calling the renderer?

"The length of the string exceeds the value set on the maxJsonLength property." when trying to convert a partial view to string

I am getting the error "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property." when trying to convert a MVC partial view to string when the payload exceeds the MaxJsonLength property.
How do I go about setting the MaxJsonLength property in this case? I've tried setting the <jsonSerialization maxJsonLength="2147483644"/> property in the web.config, as per this post, but that doesn't have any effect. I am not entirely where to go from here and looking for a bit of a guidance.
The code errors on the viewResult.View.Render(viewContext, sw); line below:
protected string ConvertViewToString(string viewName, object model)
{
string razorView = string.Empty;
if (string.IsNullOrWhiteSpace(viewName)) return razorView;
if (model != null && ViewData != null)
{
ViewData.Model = model;
}
if (ControllerContext != null)
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
if (viewResult != null)
{
using (StringWriter sw = new StringWriter())
{
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);
razorView = sw.GetStringBuilder().ToString();
}
}
}
return razorView;
}
The underlying cause of this was due to trying to load too large a result set into the Telerik Kendo grid within the partial view - the Kendo grid was throwing the exeception which is why it was being caught on the viewResult.View.Render(viewContext, sw) line. The solution was to break the result set down into smaller manageable chunks.

Render Razor view to string in ASP.NET 5

In previous versions of ASP.NET it was possible, although not very simple, to render Razor views as strings. The methods I've seem are to use a fake controller, or also to use some external engine like RazorEngine.
Now, many things are changed with ASP.NET 5 and I was wondering whether or not this is now simpler than before. So in the new version of the framework is there one straightforward way to render Razor views as strings or we still need to use the methods from the previous versions?
I use the following types injected from the IServiceProvider:
ICompositeViewEngine viewEngine;
ITempDataProvider tempDataProvider;
IHttpContextAccessor httpContextAccessor;
I render the content using the following method:
private async Task<string> RenderView(string path, ViewDataDictionary viewDataDictionary, ActionContext actionContext)
{
using (var sw = new System.IO.StringWriter())
{
var viewResult = viewEngine.FindView(actionContext, path);
var viewContext = new ViewContext(actionContext, viewResult.View, viewDataDictionary, new TempDataDictionary(httpContextAccessor, tempDataProvider), sw);
await viewResult.View.RenderAsync(viewContext);
sw.Flush();
if (viewContext.ViewData != viewDataDictionary)
{
var keys = viewContext.ViewData.Keys.ToArray();
foreach (var key in keys)
{
viewDataDictionary[key] = viewContext.ViewData[key];
}
}
return sw.ToString();
}
}
I call it like this:
var path = "~/Views/Home/Index.cshtml";
var viewDataDictionary = new ViewDataDictionary(new Microsoft.AspNet.Mvc.ModelBinding.EmptyModelMetadataProvider(), new Microsoft.AspNet.Mvc.ModelBinding.ModelStateDictionary());
var actionContext = new ActionContext(httpContextAccessor.HttpContext, new Microsoft.AspNet.Routing.RouteData(), new ActionDescriptor());
viewDataDictionary.Model = null;
var text = await RenderView(path, viewDataDictionary, actionContext);
Of course, my viewDataDictionary and actionContext variables are set by another method for encapsulation. A modification to the new ViewDataDictionary line can result in a typed Model being bound to your View if you choose.
This code uses heavy usings, I think I've listed them below. Otherwise, VS2015 is pretty good about finding them.
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
This was written under beta-3; it still builds, but some things may change. I'll try to come back here to update if it does.
There is a solution that I've used an year ago. I'm not sure if there is a new/better way to do, but it solves the problem.
public string RenderViewToString(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();
}
}

How do you convert a Razor view to a string?

I would like to use my Razor view as some kind of template for sending emails,
so I would like to "save" my template in a view, read it into controller as a string, do some necessary replacements, and then send it.
I have solution that works: my template is hosted somewhere as an HTML page, but I would like to put it into my application (i.e. in my view). I don't know how to read a view as a string in my controller.
I use the following. Put it on your base controller if you have one, that way you can access it in all controllers.
public static string RenderPartialToString(Controller controller, string viewName, object model)
{
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();
}
}
Take a look at the RazorEngine library, which does exactly what you want. I've used it before for email templates, and it works great.
You can just do something like this:
// Read in your template from anywhere (database, file system, etc.)
var bodyTemplate = GetEmailBodyTemplate();
// Substitute variables using Razor
var model = new { Name = "John Doe", OtherVar = "Hello!" };
var emailBody = Razor.Parse(bodytemplate, model);
// Send email
SendEmail(address, emailBody);

Test the function "System.Web.Mvc.ViewEngines.Engines.FindPartialView"

I want to (unit)test the function System.Web.Mvc.ViewEngines.Engines.FindPartialView and check the correct return of the HTML Code.
But every time I start the unit test, it throws an "Object reference not set to an instance of an object" exception.
I've tried to debug through the .net framework source but the debugger gets disoriented and jumps randomly / breaks with no message.
Now I want to know what element I’ve missed in the FakeControllerContext and how to fix it.
Here's my Code:
public static string RenderPartialViewToString(string viewName, object model, ControllerContext controller)
{
if (string.IsNullOrEmpty(viewName))
viewName = controller.RouteData.GetRequiredString("action");
controller.Controller.ViewData.Model = model;
using (var sw = new StringWriter())
{
//"Error: ***.Shop.UnitTests.RenderStuffTest.RenderPartialViewToStringTest-Test method threw an exception: System.NullReferenceException – Object reference not set to an instance of an object"
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller, viewName);
controller.Controller.ViewData.Model = model;
controller.Controller.ViewBag.Part = true;
var viewContext = new ViewContext(controller, viewResult.View, controller.Controller.ViewData,
controller.Controller.TempData, sw);
viewResult.View.Render(viewContext, sw);
return sw.GetStringBuilder().ToString();
}
}
And here's my test:
[TestMethod]
public void RenderPartialViewToStringTest()
{
const string viewName = "_navi";
var customersController = new ArticleController();
customersController.ControllerContext = new FakeControllerContext(customersController) { RouteData =
{
Route =
new Route(
"{language}/{controller}/{action}/{id}",
new MvcRouteHandler())
,
RouteHandler = new MvcRouteHandler()
},
};
customersController.ControllerContext.RouteData.Values.Add("language", "German");
customersController.ControllerContext.RouteData.Values.Add("controller", "Article");
customersController.ControllerContext.RouteData.Values.Add("action", "Index");
customersController.ControllerContext.RouteData.Values.Add("id", "");
var model = (...);
string actual = RenderStuff.RenderPartialViewToString(viewName, model, customersController.ControllerContext);
(...)
}
For the mocking I’ve used Rhino.Mocks and the MvcFakes from Stephenwalther.com
I think that this thread can help you, you have to mock the ViewEngine then mock the FindPartialView call.

Categories