我知道有很多类似的问题,但是在任何一个问题中我都没有得到我想要的答案。我想通过做类似以下的事情将ActionResult转换为String:
public ActionResult ProductDetail(int id)
{
// ... lots of operations that include usage of ViewBag and a Model ...
return View(product);
}
为了向客户展示产品页面,同时我也希望能够通过JSON发送它,例如:
public JsonResult ProductDetailJSON(int id)
{
// ... lots of operations that include usage of ViewBag and a Model ...
return Json(new {
html = this.ProductDetail(id).ToString(),
name = ""
// ... more properties
}, JsonRequestBehavior.AllowGet);
}
所以我将调用控制器操作并获取其响应(一个ActionResult,实际上是ViewResult),并将其转换为字符串,但使用控制器方法的所有业务代码创建它(特别是ViewBag的使用)。
编辑:关于为什么我要这样做的澄清
我需要能够在浏览器中呈现产品,因此我创建了我的控制器操作称为ProductDetail,并且运行得非常好。
之后需要具有API,该API提供给定产品的JSON对象以及视图中的一些元数据(例如已售出的单位,名称等)。其中一种选择是发送到视图的链接,仅此而已...但是,视图必须作为客户端要求内联。
为避免代码重复并保持干净,我想调用方法ProductDetail,并捕获HTML Raw响应,将其放入JSON响应中作为String。
我已经看到其他人使用此类方法呈现视图:
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();
}
}
但是这种方式不会执行控制器逻辑。