将 Addition 参数传递给 IControllerFactory.CreateController

发布于 2024-12-03 11:22:39 字数 833 浏览 2 评论 0原文

我正在使用 MEF 在 MVC3 应用程序中动态加载控制器。 在导出元数据中,我指定了两个元数据约束

EX:
    [ExportMetadata("controllerName", "APSR")]
    [ExportMetadata("controllerVersion", "1.0.0.0")]

在我的“主”mvc 应用程序中,我使用 RedirectToAction 方法(响应用户单击下拉列表)

   [HttpPost]
    public ActionResult Index(Models.HomeViewModel selected)
    {
        //ViewData.Add("Version", selected.AvailableWorkflows[int.Parse(selected.SelectedWorkflow)].Version);
        return RedirectToAction("Create", selected.AvailableWorkflows[int.Parse(selected.SelectedWorkflow)].Controller);
    }

如何将所需的版本号传递给我的控制器工厂? 由于 IControllerFactory.CreateController 方法仅参数除外:

IController IControllerFactory.CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)

I am using MEF to dynamically load controllers in an MVC3 app.
In the export metadata I am specifying two meta data constraints

EX:
    [ExportMetadata("controllerName", "APSR")]
    [ExportMetadata("controllerVersion", "1.0.0.0")]

In my "main" mvc app, I am using a RedirectToAction method (In reponse to a user click on a dropdown)

   [HttpPost]
    public ActionResult Index(Models.HomeViewModel selected)
    {
        //ViewData.Add("Version", selected.AvailableWorkflows[int.Parse(selected.SelectedWorkflow)].Version);
        return RedirectToAction("Create", selected.AvailableWorkflows[int.Parse(selected.SelectedWorkflow)].Controller);
    }

How can I pass the desired version number to my Controller factory? Since the IControllerFactory.CreateController method only excepts to paramters:

IController IControllerFactory.CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘羡 2024-12-10 11:22:39

我想您需要一些额外的路线数据,并在创建控制器时阅读这些数据。

例如,我可以将路由定义为:

routes.MapRoute(
  "APSR_Create",
  "/apsr/{version}/create",
  new {
    controller = "APSR",
    action = "Create",
    version = "1.0.0.0"
  });

现在,当我创建控制器的实例时,我可以从 RequestContext.RouteData 集合中获取该版本项:

  public IController IControllerFactory.CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
  {
    string version = requestContext.RouteData["version"];

    // Create instance using metadata lookup...
  }

您只需确保传递版本作为路由的参数。

  return RedirectToAction(
    "Create", 
    new { version = selected.AvailableWorkflows[int.Parse(selected.SelectedWorkflow)].Version });

I would imagine you need some additional route data, and reading that when creating your controller.

For instance, I could define a route as:

routes.MapRoute(
  "APSR_Create",
  "/apsr/{version}/create",
  new {
    controller = "APSR",
    action = "Create",
    version = "1.0.0.0"
  });

Now, when I create an instance of my controller, I can grab that version item from the RequestContext.RouteData collection:

  public IController IControllerFactory.CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
  {
    string version = requestContext.RouteData["version"];

    // Create instance using metadata lookup...
  }

You just need to ensure that you are passing the version as an argument to the route.

  return RedirectToAction(
    "Create", 
    new { version = selected.AvailableWorkflows[int.Parse(selected.SelectedWorkflow)].Version });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文