문제

I have an MVC app where controller A calls an internal HTTPGET method (handled by controller B). A has a view and B doesn't.

The HTTPGET in the controller B looks like this :

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

I want to use an action filter with my B controller which does the error logging for me. I do need the parameters passed with HTTP GET while logging. How can I pass accessToken and UID to the action filter such that I can log it.

What I'm looking for is something like this : The controller should be something like

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

while the action filter should do the logging

public class MyActionFilterAttribute : ActionFilterAttribute {
    public override void onActionExecuted(HttpActionExecutedContext actionExecutedContext) {
        // READ THE HTTP GET PARAMETERS AND DO THE LOGGING
    }
}
도움이 되었습니까?

해결책

You can use this:

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

다른 팁

Best way would be to Log QueryString and other Items as suggested by other answers, however if you want to access only the Method Parameters then you can do it as shown below, ActionParameters Dictionary will give you all method parameters.

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

I solved this by making the required parameters public in the controller, and reading the params directly as filterContext.Controller.publicParam.

The ActionFilter now looks like this -

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
    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top