2010-05-27 6 views
5

컨트롤러 중 하나에 기본 컨트롤러를 구현하고 싶습니다. 해당 기본 컨트롤러 내에서 현재 실행중인 ActionResult 이름을 가져올 수 있어야합니다.기본 컨트롤러에서 동작 이름을 얻으려면 어떻게해야합니까?

어떻게해야합니까?

public class HomeController : ControllerBase 
{ 
    public ActionResult Index() 
    { 

그리고;

public class ControllerBase : Controller 
{ 
    public ControllerBase() 
    { 
     //method which will get the executing ActionResult 
    } 
} 

답변

14

컨트롤러가 현재 인스턴스화 중이며 아직 동작을 호출 할 수 없으므로 컨트롤러의 생성자에서이를 알 수 없습니다. 그러나 Initialize 메서드를 무시하고 라우팅 엔진에서 작업 이름을 가져올 수 있습니다.

protected override void Initialize(RequestContext requestContext) 
{ 
    base.Initialize(requestContext); 
    var actionName = requestContext.RouteData.Values["action"]; 
} 
+0

아, 훌륭합니다! @ 대린 감사합니다. – griegs

+0

@griegs, 오신 것을 환영합니다. –

관련 문제