如何从基本控制器获取操作名称?
我想在我的一个控制器上实现一个基本控制器。在该基本控制器中,我希望能够获取当前执行的 ActionResult 名称。
我该怎么做呢?
public class HomeController : ControllerBase
{
public ActionResult Index()
{
和;
public class ControllerBase : Controller
{
public ControllerBase()
{
//method which will get the executing ActionResult
}
}
I'd like to implement a base controller on one of my controllers. Within that base controller, I'd like to be able to get the current executing ActionResult name.
How would I go about doing this?
public class HomeController : ControllerBase
{
public ActionResult Index()
{
And;
public class ControllerBase : Controller
{
public ControllerBase()
{
//method which will get the executing ActionResult
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法在控制器的构造函数中知道这一点,因为控制器当前正在实例化,并且尚未调用任何操作。但是,您可以覆盖 Initialize 方法并从路由引擎获取操作名称:
You can't know this in the constructor of the controller as the controller is currently being instantiated and no action could be called yet. However you could override the Initialize method and fetch the action name from the routing engine: