Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.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
C# 获取OnActionExecuting上的操作参数_C#_.net_Asp.net Core_Asp.net Core Mvc - Fatal编程技术网

C# 获取OnActionExecuting上的操作参数

C# 获取OnActionExecuting上的操作参数,c#,.net,asp.net-core,asp.net-core-mvc,C#,.net,Asp.net Core,Asp.net Core Mvc,使用.NET核心MVC# 我有一个控制器: [ServiceFilter(typeof(MyCustomFilter))] public class HomeController : Controller { public IActionResult Index(string type) { } } 在我的过滤器中,我想获得type的字符串值,因为它是作为查询字符串传递的 我的过滤器是: [AttributeUsage(AttributeTargets.Class

使用.NET核心MVC#

我有一个控制器:

[ServiceFilter(typeof(MyCustomFilter))]
public class HomeController : Controller
{
    public IActionResult Index(string type)
    {

    }
}
在我的过滤器中,我想获得
type
的字符串值,因为它是作为查询字符串传递的

我的过滤器是:

  [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class MyCustomFilter: ActionFilterAttribute
{
   public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
          //this is always null
           string id = filterContext.RouteData.Values["type"].ToString();
    }
}

我有什么遗漏吗?因为我读过几篇文章,他们提到了上述方法。

对于查询字符串中传递的参数,请使用:

string id = filterContext.Request.Query["type"];

我相信你可以用下面这样的东西。我在日志过滤器中使用类似于下面的内容。日志过滤器代码也在下面。您还可以迭代集合

string id = filterContext.ActionParameters["Type"];
遍历所有参数并将其聚合为日志字符串,最终写入文件

logString = filterContext.ActionParameters.Aggregate(logString, (current, p) => current + (p.Key + ": " + p.Value + Environment.NewLine));

谢谢,这很有效。修改如下,如果(filterContext.HttpContext.Request.Cookies[“type”]!=null){}