Wouter de Kort によるこの記事Rendering an ASP.NET MVC View to a string in a Web Api Controllerを確認してください。
public static string RenderViewToString(string controllerName, string viewName,
object viewData)
{
HttpContextBase contextBase = new HttpContextWrapper(HttpContext.Current);
var routeData = new RouteData();
routeData.Values.Add("controller", controllerName);
var controllerContext = new ControllerContext(contextBase, routeData,
new EmptyController());
var razorViewEngine = new RazorViewEngine();
var razorViewResult = razorViewEngine.FindView(controllerContext, viewName,
"", false);
var writer = new StringWriter();
var viewContext = new ViewContext(controllerContext, razorViewResult.View,
new ViewDataDictionary(viewData), new TempDataDictionary(), writer);
razorViewResult.View.Render(viewContext, writer);
return writer.ToString();
}
private class EmptyController : ControllerBase
{
protected override void ExecuteCore() { }
}
//You can simply call this from anywhere in your Web Api Controller:
RenderViewToString("controller", "view", model)