如何指示控制器中的方法不是操作方法?

How to indicate a method in a controller is not an action method?

我有一种情况想使用页面特定的控制器。在那个控制器中,我有一个动作方法和一堆辅助方法。在现实生活中,辅助方法是从 BaseController 继承的,但为了简单起见,我们假设我的控制器 class 中直接只有一个辅助方法,如下所示:

[Route("/dev/test")]
public class TestController : Controller {

    public IActionResult Get() {
        return UnprocessedEntityResult();
    }

    //Some helper method that I don't want to be considered an 
    //action method by the routing engine.
    public IActionResult UnprocessedEntityResult() {
        return StatusCode(StatusCodes.Status422UnprocessableEntity);
    }
}

我特别想使用基于属性的路由,我希望在 class 级别指定基于属性的路由。

鉴于上述情况,访问/dev/test路由时将抛出AmbiguousActionException,表明

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

App.Dev.TestController.Get
App.Dev.TestController.UnprocessedEntityResult

如何告诉路由引擎 UnprocessedEntityResult() 不是一个操作方法?我假设一定有一些属性可以应用于该方法,但我一直无法找到它。

查找 [NonAction] 属性。

Indicates that a controller method is not an action method.

[Route("/dev/test")]
public class TestController : Controller {
    [HttpGet]
    public IActionResult Get() {
        return UnprocessedEntityResult();
    }

    [NonAction]
    public IActionResult UnprocessedEntityResult() {
        return StatusCode(StatusCodes.Status422UnprocessableEntity);
    }
}

或者您也可以使操作受到保护。

[Route("/dev/test")]
public class TestController : Controller {
    [HttpGet]
    public IActionResult Get() {
        return UnprocessedEntityResult();
    }

    protected IActionResult UnprocessedEntityResult() {
        return StatusCode(StatusCodes.Status422UnprocessableEntity);
    }
}

它将对派生类型可见,但不会混淆路由 table