我想在我的一个控制器上实现一个基本控制器。在该基本控制器中,我希望能够获取当前执行的 ActionResult 名称。
我该怎么做呢?
public class HomeController : ControllerBase
{
public ActionResult Index()
{
And;
public class ControllerBase : Controller
{
public ControllerBase()
{
//method which will get the executing ActionResult
}
}
您无法在控制器的构造函数中知道这一点,因为控制器当前正在实例化,并且尚未调用任何操作。但是你可以覆盖初始化方法并从路由引擎获取操作名称:
protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
var actionName = requestContext.RouteData.Values["action"];
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)