Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 将动态变量从控制器传递到自定义操作筛选器';s-HTTPGET方法_Asp.net_.net_Asp.net Mvc 4_Action Filter_Custom Action Filter - Fatal编程技术网

Asp.net 将动态变量从控制器传递到自定义操作筛选器';s-HTTPGET方法

Asp.net 将动态变量从控制器传递到自定义操作筛选器';s-HTTPGET方法,asp.net,.net,asp.net-mvc-4,action-filter,custom-action-filter,Asp.net,.net,Asp.net Mvc 4,Action Filter,Custom Action Filter,我有一个MVC应用程序,其中控制器A调用内部HTTPGET方法(由控制器B处理)。A有一个视图,B没有 控制器B中的HTTPGET如下所示: [HttpGet] public String GetToken(string accessToken, string UID) { .... // Log errors and other metrics return someToken; } 我想在我的B控制器上使用一个动作过滤器,它为我记录错误。我确实需要在日志记录时通过

我有一个MVC应用程序,其中控制器A调用内部HTTPGET方法(由控制器B处理)。A有一个视图,B没有

控制器B中的HTTPGET如下所示:

[HttpGet]
public String GetToken(string accessToken, string UID)  {
    ....
    // Log errors and other metrics
    return someToken;
}
我想在我的B控制器上使用一个动作过滤器,它为我记录错误。我确实需要在日志记录时通过HTTP GET传递的参数。如何将accessToken和UID传递给操作筛选器以便记录它

我要找的是这样的东西: 控制器应该类似于

[MyActionFilter]
[HttpGet]
public String GetToken(string accessToken, string UID)  {
        ....
        return someToken;
    }
而动作过滤器应该做日志记录

public class MyActionFilterAttribute : ActionFilterAttribute {
    public override void onActionExecuted(HttpActionExecutedContext actionExecutedContext) {
        // READ THE HTTP GET PARAMETERS AND DO THE LOGGING
    }
}
您可以使用以下选项:

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

我通过在控制器中公开所需的参数,并将参数直接读取为
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
    }

最好的方法是记录查询字符串和其他答案建议的其他项目, 但是,如果只想访问方法参数,则可以这样做 如下所示,ActionParameters字典将为您提供所有方法参数

public class MyActionFilterAttribute : ActionFilterAttribute {
    public override void OnActionExecuted
       (HttpActionExecutedContext filterContext) {
        foreach (KeyValuePair<string,object> item 
            in filterContext.ActionParameters)
        {
            //print item.Key
            //print item.Value
        }
    }
}
公共类MyActionFilterAttribute:ActionFilterAttribute{
已执行公共覆盖无效操作
(HttpActionExecutedContext筛选器上下文){
foreach(KeyValuePair项
在filterContext.ActionParameters中)
{
//打印项目。键
//打印项目。值
}
}
}

Bounty仍然适用于最佳答案@PKKG-我不想使用ViewBag,它与体系结构不匹配。我说的控制器没有视图。对于第二种方法,这只适用于常量。你不能像那样传递动态变量。@divyanshm:谢谢你,阿卡什。我考虑过查询字符串选项,但我也必须发送查询字符串+其他参数。这就是为什么我使用我提到的方法。我也会试试这个。