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

时间:2010-05-21 00:45:01

标签: c# asp.net-mvc-2

我有一个控制器方法:

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

用于95%的可能调用。对于其他5%我需要在传递到外观之前操纵id的值。我想在同一个控制器中创建一个单独的方法来执行该操作,然后调用this(Details)方法。

该方法的签名是什么样的?调用主要Details方法的语法是什么?

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

    [syntax to call Details(manipulatedID)]
}

mny thx

2 个答案:

答案 0 :(得分:1)

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

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

//假设路由上存在{id}(通常在默认路由中)

答案 1 :(得分:0)

如果要直接调用操纵器方法作为控制器上的操作,则可以执行以下操作:

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

如果所有访问都是通过详细信息操作,那么您可以执行此操作:

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 ...; }