Search code examples
asp.net.netasp.net-mvc-4action-filtercustom-action-filter

Passing dynamic variable to custom action filter from controller's HTTPGET method


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

Solution

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