简体   繁体   English

在同一控制器上调用不同方法的控制器方法

[英]A controller method that calls a different method on the same controller

I have a controller method: 我有一个控制器方法:

  public ActionResult Details(int id)
  {
      Order order = OrderFacade.Instance.Load(id);
      return View(order);
  }

that is used for 95% of possible invocations. 用于95%的可能调用。 For the other 5% i need to manipulate the value of id before passing to the facade. 对于其他5%,我需要在传递到立面之前操纵id的值。 I'd like to create a separate method within this same controller that executes that manipulation and then calls this (Details) method. 我想在同一控制器内创建一个单独的方法,该方法执行该操作,然后调用此(详细信息)方法。

What would the signature of that method look like? 该方法的签名是什么样的? What is the syntax to call the main Details method? 调用主Details方法的语法是什么?

public ??? ManipulatorMethod(int id)
{
    [stuff that manipulates id]

    [syntax to call Details(manipulatedID)]
}

mny thx mny thx

public ActionResult ManipulatorMethod(int id) 
{ 
    //[stuff that manipulates id] 
    int _id = id++;

    //[syntax to call Details(manipulatedID)] 
    return RedirectToAction("Details", new { id = _id });
} 

//assuming that {id} exists on the route (usually in the default route) //假设{id}在路由上(通常在默认路由中)

If you will invoke the manipulator method directly as an action on the controller you can do this: 如果您将直接调用manipulator方法作为对控制器的操作,则可以执行以下操作:

public ActionResult ManipulatorMethod( int id )
{
    // Do something to id
    return Details( id );
}

If all access will be through the Details action then you can do this: 如果所有访问都将通过“详细信息”操作进行,则可以执行以下操作:

public ActionResult Details( int id )
{
    if( IdNeedsManipulation( id ) )
        id = ManipulateId( id );

    return View( id );
}

private int ManipulateId( int id )
{
    // Do something to id
    return id;
}

private bool IdNeedsManipulation( int id ) { return ...; }

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

相关问题 SetActualResponseType使用相同方法在同一控制器中使用不同的路由 - SetActualResponseType with same method in the same controller with different routes 使用POST重新路由到同一控制器中的不同方法 - Rerouting to different method in same controller with POST 如何为具有不同参数的同一Controller方法设置不同的路由? - How to set different routes for the same Controller method with different parameters? 来自控制器的C#接口方法调用 - C# Interface Method calls from a controller @ Url.Action(“ Action”,“ Controller”)调用具有相同名称的post和get方法 - @Url.Action(“Action”,“Controller”) calls both post and get method with same name 控制器方法不更新(每次都是相同的结果) - Controller method not updating(same result every time) 控制器中的相同方法接受 GET 和 POST - Same method in controller accept GET and POST 重定向到Sitecore中的相同控制器和操作方法 - redirect to the same controller and action method in sitecore 是否可以将多个动作分配给同一控制器方法? - Is it possible to assign multiple actions to the same controller method? 在同一个MVC 6控制器中组合API控制器调用和控制器调用 - Combining API controller calls and controller calls in same MVC 6 controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM