从AuthorizationHandlerContext对象获取控制器和动作名称
问题描述:
我有一个自定义需求处理程序,它接受 AuthorizationHandlerContext上下文
参数
Hi I have a custom requirement handler with accepts the AuthorizationHandlerContext context
parameter
当我调试时,我可以看到上下文对象包含
Context.Resources.ActionDescription.ActionName
When i debug, i can see that the context object contains Context.Resources.ActionDescription.ActionName
但是在编写代码时,我无法超越
Context.Resources
But when writing the code i cant go beyond Context.Resources
似乎较低的水平没有被暴露。我想获取调用处理程序的操作名称和控制器名称。我该怎么做?
Seems the lower levels are not exposed. I want to get the action name and controller name that called the handler. How do i do this?
答
var mvcContext = context.Resource as AuthorizationFilterContext;
var descriptor = mvcContext?.ActionDescriptor as ControllerActionDescriptor;
if (descriptor != null)
{
var actionName = descriptor.ActionName;
var ctrlName = descriptor.ControllerName;
}