您可以通过 ActionExecutedContextActionDescriptor 属性获取当前执行的 ActionDescriptor,然后通过 ActionDescriptorRouteValues 属性获取路由值。要获取路由参数,可以使用 ActionExecutedContextActionArguments 属性,该属性包含路由参数和查询字符串参数的键值对。以下是一个示例代码:

public class MyActionFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        var actionDescriptor = context.ActionDescriptor;
        var routeValues = actionDescriptor.RouteValues;

        // 获取路由名
        var routeName = routeValues["controller"] + "." + routeValues["action"];

        // 获取路由参数
        var actionArguments = context.ActionArguments;
        var id = actionArguments["id"];
    }
}

在上面的示例中,我们首先获取当前执行的 ActionDescriptor,然后从中提取路由值。然后,我们从 ActionExecutedContextActionArguments 属性中获取路由参数和查询字符串参数的键值对。最后,我们可以使用这些值来完成自己的逻辑。

MVC ActionFilterAttribute OnActionExecuted 获取路由名和参数

原文地址: https://www.cveoy.top/t/topic/oFg9 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录