简体   繁体   English

从不同的控制器直接调用 ASP.NET 控制器操作

[英]Directly call ASP.NET controller action from a different controller

I have a situation where I need to redirect to an ASP.NET MVC action in a different controller.我有一种情况,我需要重定向到不同控制器中的 ASP.NET MVC 操作。 I can't use RedirectToAction because I must POST the action's parameters to keep them out of the URL.我不能使用 RedirectToAction,因为我必须 POST 操作的参数以将它们保留在 URL 之外。

I attempted to instantiate and call the other controller's action directly like this:我试图像这样直接实例化并调用另一个控制器的操作:

OtherController myOtherController = new OtherController();
myOtherController.ControllerContext = new ControllerContext(this.ControllerContext.RequestContext, myOtherController);
return await myOtherController.Edit(myGuid);

When I do this, the other controller's code executes, but I end up with this error:当我这样做时,另一个控制器的代码会执行,但我最终遇到了这个错误:

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.OtherModel_BBCEF7C9378F4C4F097CC08FA2E508B8BD8D865E33093E31959919087A31348E', but this dictionary requires a model item of type 'ThisModel'.传递到字典中的模型项的类型为“System.Data.Entity.DynamicProxies.OtherModel_BBCEF7C9378F4C4F097CC08FA2E508B8BD8D865E33093E31959919087A31348E”,但此字典需要“ThisModel”类型的模型项。

Does anyone know if I can get this working using the current approach?有谁知道我是否可以使用当前的方法来完成这项工作? Assuming I must use an HTTP POST action and cannot have parameters in the URL, is there a different approach that you would recommend to achieve this result (other than combining the controllers)?假设我必须使用 HTTP POST 操作并且 URL 中不能有参数,您是否建议采用不同的方法来实现此结果(除了组合控制器)?
Edit:编辑:
Note that I don't think I can post directly from the client because I would need to nest Html.BeginForm .请注意,我认为我不能直接从客户端发布,因为我需要嵌套Html.BeginForm

You should really just be using RedirectToAction to push the browser to the action you want instead of trying to do it like this.您真的应该只是使用 RedirectToAction 将浏览器推送到您想要的操作,而不是尝试这样做。 As you can see from @Andrei Olariu code a lot of things happen under the hood during the construction of your controller (DI then context and parameter mappings) that really shouldn't be done manually and can easily be screwed up leading to hours wasted wondering why certain things are not behaving as expected.正如您从@Andrei Olariu 代码中看到的,在构建控制器(DI 然后是上下文和参数映射)的过程中,很多事情发生在幕后,这些事情真的不应该手动完成,并且很容易搞砸,导致浪费数小时的疑惑为什么某些事情没有按预期运行。

I hope I understood what you're trying to do.我希望我明白你想要做什么。 This is what I'm successfully using to execute an action on a different controller.这是我成功地用来在不同的控制器上执行操作的方法。

private void ExecuteErrorAction(string action, HttpContextWrapper httpContext, Exception exception)
{
    var routeData = new RouteData();
    routeData.Values["controller"] = "Error";
    routeData.Values["action"] = action;
    routeData.Values["exception"] = exception;

    IController errorController = DependencyResolver.Current.GetService<ErrorController>();
    var context = new RequestContext(httpContext, routeData);
    errorController.Execute(context);
}

I think that in your case this could look like:我认为在您的情况下,这可能如下所示:

private void ExecuteOtherAction(string myGuid, HttpContextWrapper httpContext)
{
    var routeData = new RouteData();
    routeData.Values["controller"] = "OtherController";
    routeData.Values["action"] = "Edit";
    routeData.Values["myGuid"] = myGuid;

    IController otherCntroller = DependencyResolver.Current.GetService<OtherController>();
    var context = new RequestContext(httpContext, routeData);
    otherCntroller.Execute(context);
}

This is assuming that your Edit action on the OtherController takes a string called myGuid as a parameter.这是假设你Edit的动作OtherController需要一个称为串myGuid作为参数。

Let me know if this helps.如果这有帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用另一个控制器,并对调用的控制器操作MVC ASP.NET使用不同的布局 - Call another controller and use a different Layout for the called controller action MVC ASP.NET ASP.NET | 从 MVC 中的 controller 调用另一个控制器的操作 - ASP.NET | Call another controller's action from a controller in MVC ASP.NET MVC从同一控制器发布到控制器操作 - ASP.NET MVC post to controller action from same controller 如何从Html.Action中检索控制器和操作名称,该操作是从ASP.NET MVC中的不同控制器和操作呈现的 - How to retrieve controller and action names from Html.Action that is rendered from different controller and action in ASP.NET MVC ASP.NET MVC 4从另一个控制器调用一个控制器方法 - ASP.NET MVC 4 call a controller method from another controller asp.net如何在控制器动作中访问ajax调用数据 - asp.net how to access ajax call data in the controller action ASP.NET MVC AJAX 调用应用程序/控制器/操作? - ASP.NET MVC AJAX call Application/Controller/Action? ASP.NET MVC以编程方式初始化Controller并调用操作 - ASP.NET MVC initialize Controller and call action programatically ASP.NET MVC-在Bootstrap模式中使用表单从控制器调用操作 - ASP.NET MVC - Using a Form in a Bootstrap Modal to call an Action from a Controller ASP.NET MVC调用带有JavaScript函数参数的Controller Action - ASP.NET MVC call Controller Action with parameter from javascript function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM