2

컨트롤러 A가 내부 HTTPGET 메서드 (컨트롤러 B에서 처리)를 호출하는 MVC 응용 프로그램이 있습니다. A는보기가 있고 B는 그렇지 않습니다.컨트롤러의 HTTPGET 메서드에서 사용자 지정 작업 필터에 동적 변수 전달

컨트롤러 B의 HTTPGET은 다음과 같습니다

[HttpGet] 
public String GetToken(string accessToken, string UID) { 
    .... 
    // Log errors and other metrics 
    return someToken; 
} 

내가 나를 위해 오류 로깅을 수행 내 B 컨트롤러와 액션 필터를 사용하고 싶습니다. 로깅하는 동안 HTTP GET으로 전달 된 매개 변수가 필요합니다. 어떻게하면 actionToken과 UID를 액션 필터에 건네 주어서 기록 할 수 있습니까? 액션 필터가 로깅을 수행해야합니다 동안

public class MyActionFilterAttribute : ActionFilterAttribute { 
    public override void onActionExecuted(HttpActionExecutedContext actionExecutedContext) { 
     // READ THE HTTP GET PARAMETERS AND DO THE LOGGING 
    } 
} 
+0

** [이전 검색어를 확인하십시오. 왜 50 달러를 썼습니까?] (http://stackoverflow.com/questions/18209735/how-do-i-pass-variables-to-a-custom-actionfilter-in-asp-net-mvc-app) * * –

+0

@PKKG - ViewBag을 사용하고 싶지 않고 아키텍처와 잘 어울리지 않습니다. 내가 말하는 컨트롤러는 시야가 없다. 그리고 두 번째 방법은 상수에 대해서만 유효합니다. 당신은 그런 동적 변수를 전달할 수 없습니다. – divyanshm

+0

@divyanshm : ** [어때요?] (http://stackoverflow.com/questions/18286313/how-to-return-404-page-in-asp-net-mvc-when-query-string-parameters- is-incorrect/18287393 # 18287393) ** –

답변

3

당신이 사용할 수

[MyActionFilter] 
[HttpGet] 
public String GetToken(string accessToken, string UID) { 
     .... 
     return someToken; 
    } 

같은 컨트롤러는해야 뭔가 :

내가 무엇을 찾고

이 같은 것입니다

public class MyActionFilterAttribute : ActionFilterAttribute { 
    public override void onActionExecuted(
            ActionExecutedContext actionExecutedContext) { 
     // READ THE HTTP GET PARAMETERS AND DO THE LOGGING 
     actionExecutedContext.HttpContext.Request.QueryString; // HTTP GET 
     actionExecutedContext.HttpContext.Request.Params; 
     // HTTP GET/POST/COOKIE PARAMETERS As Key Value List 
    } 
} 
-1

필자는 공동으로 필요한 매개 변수를 공개함으로써이를 해결했습니다. 매개 변수를 직접 읽고 filterContext.Controller.publicParam으로 읽습니다.

ActionFilter 이제 다음과 같습니다 -

public override void OnActionExecuted(ActionExecutedContext filterContext) 
    { 
     var thisController = ((myController)filterContext.Controller); 

     // public function in the controller that returns the required object 
     RequiredParameters requiredParams = 
            thisController.getParametersForLogging(); 

     // read the variables from requiredParams object and do the logging 
    } 
+0

Bounty는 최선의 답변을 위해 계속 사용할 수 있습니다! – divyanshm

+0

@Downvoters : comments please –

1

가장 좋은 방법은 다른 답변에 의해 제안 당신이 그것을 할 수있는 당신 만 메서드 매개 변수에 액세스하려는 경우, 을 QueryString을 다른 항목을 로그하는 것 아래와 같이 ActionParameters Dictionary는 모든 메소드 매개 변수를 제공합니다.

public class MyActionFilterAttribute : ActionFilterAttribute { 
    public override void OnActionExecuted 
     (HttpActionExecutedContext filterContext) { 
     foreach (KeyValuePair<string,object> item 
      in filterContext.ActionParameters) 
     { 
      //print item.Key 
      //print item.Value 
     } 
    } 
} 
+0

감사합니다. Akash. 쿼리 문자열 옵션을 고려했지만 쿼리 문자열 + 다른 매개 변수도 보내야했습니다. 그래서 내가 언급 한 접근 방식을 사용했습니다. 나는 이것을 시도 할 것이다. – divyanshm

관련 문제