I would like to write a Web Api controller action that would send an email depending on results. I would like to use an MVC View or Partial view with a model of data to render t
If you don´t want to go with the RazorEngine approach suggested in the comments, you could define a class like this:
public static class ViewUtil
{
public static string RenderPartial(string partialName, object model)
{
var sw = new StringWriter();
var httpContext = new HttpContextWrapper(HttpContext.Current);
// point to an empty controller
var routeData = new RouteData();
routeData.Values.Add("controller", "EmptyController");
var controllerContext = new ControllerContext(new RequestContext(httpContext, routeData), new EmptyController());
var view = ViewEngines.Engines.FindPartialView(controllerContext, partialName).View;
view.Render(new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), sw), sw);
return sw.ToString();
}
}
class EmptyController : Controller { }