Composable services with ASP.NET WebApi…sort of

A while ago I had blogged about Service Composition using ServiceStack and MEF. With ServiceStack no longer free, I figured I’d try the same thing using ASP.NET Web Api from Big Mike. The result was…not as good.
Continue reading “Composable services with ASP.NET WebApi…sort of”

Composable services with ASP.NET WebApi…sort of

rendering view as a string

I can’t remember who the original writer of this code was, so forgive me for plagiarizing. As part of a project I’m involved in (see previous post), I needed to output a rendered html partial view so as to append to a div element. Here’s the code that helped me achieve this.

protected string RenderPartialViewToString(string viewName, object model)
{
if (string.IsNullOrEmpty(viewName))
viewName = ControllerContext.RouteData.GetRequiredString("action");
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);
return sw.GetStringBuilder().ToString();
}
}

view raw
gistfile1.cs
hosted with ❤ by GitHub

I hope it helps.

rendering view as a string