2010-05-26 2 views

답변

17

또한 단순히 때문에 같은 방법으로 바로 리디렉션 수 : 컨트롤러와 같은

public class ThisController 
{ 
    public ActionResult Index() 
    { 
     return RedirectToAction("OtherMethod", "OtherController"); 
    } 
} 
+0

예, "OtherMethod"에서 List를 반환하고 싶습니까? var result = RedirectToAction은 List를 전달하지 않습니다 ... – eMi

10

기술적으로 그렇습니다. 컨트롤러의 정적 메서드를 호출하거나 컨트롤러의 인스턴스를 초기화하여 해당 인스턴스 메서드를 호출 할 수 있습니다.

그러나 이는 거의 의미가 없습니다. 컨트롤러의 메소드는 라우팅 엔진에 의해 간접적으로 호출됩니다. 다른 컨트롤러의 동작 방법을 직접 호출해야한다고 생각하면 할 일을 다시 디자인해야한다는 신호입니다.

public class ThisController { 
    public ActionResult Index() { 
    var other = new OtherController(); 
    other.OtherMethod(); 
    //OR 
    OtherController.OtherStaticMethod(); 
    } 
} 

당신은 또한 더 의미가 다른 컨트롤러로 리디렉션 수 :

+2

동의. 다른 컨트롤러를 호출하는 것보다는'RedirectToRouteResult'를 리턴하는 것이 더 낫습니다. –

7

음, 사실 다른 컨트롤러에서 인스턴스 메서드를 호출하거나 해당 컨트롤러 유형 떨어져 정적 메소드를 호출하는 방법은 여러 가지가 있습니다 .

public class ThisController { 
    public ActionResult Index() { 
    return RedirectToRoute(new {controller = "Other", action = "OtherMethod"}); 
    } 
} 

또는 공용 코드를 자체 클래스로 리팩토링하면 훨씬 의미가 있습니다.

public class OtherClass { 
    public void OtherMethod() { 
    //functionality 
    } 
} 

public class ThisController { 
    public ActionResult Index() { 
    var other = new OtherClass(); 
    other.OtherMethod(); 
    } 
} 
1

그래서 예, 우리는 그것을 할 수 있습니다 단지 클래스입니다. ControllerName objController=new ControllerName(); objController.methodName(parameters)

1

이 시도 - 우리는 직접 return RedirectToAction("MethodName", "ControllerName");

2.By 만드는 객체를 redirecting- 다음 ways- 1.By의 일부가 그것을 할 수 있습니다.

var ctrl= new MyController(); 
ctrl.ControllerContext = ControllerContext; 
//call action 
return ctrl.Action(); 
관련 문제