【发布时间】:2012-05-30 12:30:47
【问题描述】:
问题:
我需要的是类似于 @Html.RenderAction("action","controller") 的东西,但我可以在不同的控制器中使用它。例如:
public class FirstController : Controller
{
public ActionResult GetErDone()
{
return View();
}
}
public class SecondController : Controller
{
public ActionResult Index()
{
ActionResult coolResult =
Helper.GetActionResult("GetErDone", "FirstController");
return View();
}
}
进展:
我已经开始剥离/改革实际的@Html.Action() 方法,但它确实有太多的内部辅助依赖项,我认为这不应该那么难。到目前为止我所拥有的是:
private void TestCreateActionFromDifferentController(RouteValueDictionary routeValues, string controllerName, string actionName)
{
var httpContext = this.HttpContext;
var routeData = this.RouteData;
routeData.Values["action"] = (object)actionName;
if (!string.IsNullOrEmpty(controllerName))
routeData.Values["controller"] = (object)controllerName;
IController myController = ControllerBuilder.Current.GetControllerFactory().CreateController(new RequestContext(httpContext, routeData) , "staticpages");
}
这当然可以部分工作(减去区域数据令牌等许多缺点),但除了反射之外没有其他方法可以到达 MyActionResult 对象。
总结:
在控制器内部时,有什么方法可以从在不同控制器上定义的动作中获取 ActionResult?
更新:
更具体地说,我正在尝试使用 System.Web.Mvc.Html.ChildActionExtensions.ChildActionMvcHandler 之类的东西,但它不会执行,而是返回 ActionResult
【问题讨论】:
-
感谢链接,但是:我不需要
Render和View,我需要创建一个ActionResult对象的实例(更类似于此是呈现一个动作 -与渲染视图仍然不同)。另外,我不需要返回一个字符串,我需要返回一个 ActionResult(它需要 controllerContext 因此它不能被重构)。这是非常具体和非常不同的 -
你到底想用这个来完成什么?您是否尝试将多个操作混搭在一起?
-
主要设计原因是我想在运行时管理依赖项。想象一个依赖解析层,它位于表示层之前(在发送到视图之前)
标签: asp.net-mvc reflection asp.net-mvc-routing controller-factory