ASP.NET MVC初始化控制器和编程呼吁采取行动 [英] ASP.NET MVC initialize Controller and call action programatically

查看:326
本文介绍了ASP.NET MVC初始化控制器和编程呼吁采取行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立一个计划的作业控制器 - 这些工作将调用控制器动作捕捉的结果。我宁愿这一切都发生在后台,不涉及HTTP调用。



它有点像单元测试会发生什么 - 例如:

  VAR控制器=新的TestController(); 
VAR的结果= controller.Index(TestParameter)作为的ViewResult;



的问题是在本实施例的控制器和动作不是动态的,没有人知道如何初始化控制器和调用与控制器和行动作为字符串paramater名称的行动?比如 -

 公众的ActionResult运行(字符串controllerName,串actionName)
{
无功控制器=新控制器(controllerName);
VAR的结果= controller.action(actionName)(TestParameter)作为的ViewResult。
返回内容(结果);
}


解决方案

使用的ControllerFactory ActionDescriptor 来动态地执行操作沿>:

 公众的ActionResult运行(字符串controllerName,串actionName)
{
//获取控制
VAR ctrlFactory = ControllerBuilder.Current.GetControllerFactory();
VAR CTRL = ctrlFactory.CreateController(this.Request.RequestContext,controllerName)作为控制器;
变种ctrlContext =新的ControllerContext(this.Request.RequestContext,CTRL);
变种ctrlDesc =新ReflectedControllerDescriptor(ctrl.GetType());

//获取行动
VAR actionDesc = ctrlDesc.FindAction(ctrlContext,actionName);

//执行
VAR的结果= actionDesc.Execute(ctrlContext,新的字典<字符串对象>
{
{参数名称,TestParameter}
})为的ActionResult;

//返回其他行动结果作为当前动作的结果
返回结果;
}

请参阅的 MSDN


I am building a scheduled jobs controller - these jobs will call a Controller and an Action capturing the result. I would rather have all this happen on the backend and not involve http calls.

Its kind of like what happens with Unit testing - for instance:

var controller = new TestController();
var result = controller.Index("TestParameter") as ViewResult;

The issue is in this example the controller and action are not dynamic, does anyone know how to initialize a controller and call an action with the name of the controller and action as string paramater? Such as -

public ActionResult run(string controllerName, string actionName)
{
    var controller = new Controller(controllerName);
    var result = controller.action(actionName).("TestParameter") as ViewResult;
    return Content(result);
}

解决方案

Use the ControllerFactory along with the ActionDescriptor to dynamically execute the action:

public ActionResult Run(string controllerName, string actionName)
{
    // get the controller
    var ctrlFactory = ControllerBuilder.Current.GetControllerFactory();
    var ctrl = ctrlFactory.CreateController(this.Request.RequestContext, controllerName) as Controller;
    var ctrlContext = new ControllerContext(this.Request.RequestContext, ctrl);
    var ctrlDesc = new ReflectedControllerDescriptor(ctrl.GetType());

    // get the action
    var actionDesc = ctrlDesc.FindAction(ctrlContext, actionName);

    // execute
    var result = actionDesc.Execute(ctrlContext, new Dictionary<string, object>
    {
        { "parameterName", "TestParameter" }
    }) as ActionResult;

    // return the other action result as the current action result
    return result;
}

See MSDN

这篇关于ASP.NET MVC初始化控制器和编程呼吁采取行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆