从一个 ActionResult 方法切换到另一个方法并每次触发 ActionFilterAttribute 的正确方法是什么?

What is the proper way to hand off from one ActionResult method to another and trigger the ActionFilterAttribute each time?

我正在尝试在 return ActionResult 的控制器中编写一些方法。每个都有一个属性,在允许用户使用该方法之前应该 运行 一些验证逻辑。在一种方法中,如果我检测到某种情况,我会尝试将数据移交给另一种方法以完成处理和 return ActionResult。我发现的是,在执行此操作时,正在输入第二种方法,而没有首先在属性中进行验证。我想知道这是否是传递控制以生成 ActionResult 的错误方式,以及我可以做些什么来确保我的属性每次都被命中。考虑以下示例。

[SpecialActionFilterAttribute(ValidationRequirement1)]
public ActionResult Index(int id, bool handleWithOtherMethod)
{
    MyViewModelType viewModel = this.ModelRepository.GetModel(id);

    if (handleWithOtherMethod)
    {
        return Index(viewModel);
    }
    Response.StatusCode = 200;
    return View(viewModel);
}

[SpecialActionFilterAttribute(ValidationRequirement2)]
public ActionResult Index(MyViewModelType viewModel)
{
    viewModel.SomeSpecialProperty = "Some special value";
    Response.StatusCode = 200;
    return View(viewModel);
}

当我尝试上面的代码时,似乎 可以正确执行,但是从第一个 ActionResult 方法转到第二个时,属性中的代码没有输入。此切换的全部要点是在特定情况为真时通过 ActionFilterAttribute 代码强制执行。这是不好的形式吗?我的方法应该是什么?

中止!这不是一个好方法。