C# 获取OnActionExecuting上的操作参数
使用.NET核心MVC# 我有一个控制器: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
[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){}