如何从嵌套操作方法调用 WebAPI 中的验证中跳过自定义属性



In an Web api Action controller.每个操作方法都使用属性扩展操作筛选器属性进行修饰,以便进行安全验证。

它具有嵌套的操作方法调用。由于其嵌套调用,OnActionExecution被多次调用以进行验证。

是否可以只检查一次 OnActionExecute 并仅跳过对子操作方法调用的检查?

public class WebApiController
{
[CustomAttribute]
public IActionResult ActionMethod1()
{
WebApiController222 obj = new WebApiController222()
obj.ActionMethod2(); // Calling to second Action Methods
}
}

public class WebApiController222
{

[CustomAttribute]
public IActionResult ActionMethod2()
{
//source Code
}
}


// CustomAttribute
public class CustomAttribute : ActionFilterAttribute
{  
public override void OnActionExecuting(ActionExecutingContext context()
{
//Some Code
}
}

我希望嵌套的 api 调用 (ActionMethod2( 应该跳过 OnActionExecute 被调用。

不要直接调用 ActionMethod2。将 ActionMethod2 的内容放入一个函数 (ActionMethod2Internal( 中,并从两个位置调用它。

Public class WebApiController
{
[CustomAttribute]
public IActionResult ActionMethod1()
{
WebApiController222 obj = new WebApiController222()
return obj.ActionMethod2Internal(); // Calling to second Action Methods
}
}
public class WebApiController222
{
[CustomAttribute]
public IActionResult ActionMethod2()
{
return ActionMethod2Internal();
}
public IActionResult ActionMethod2Internal()
{
//source code
}
}
// CustomAttribute
public class CustomAttribute : ActionFilterAttribute
{  
public override void OnActionExecuting(ActionExecutingContext context()
{
//Some Code
}
}

最新更新