如何从基本控制器获取操作名称? [英] How do I get the action name from a base controller?

查看:148
本文介绍了如何从基本控制器获取操作名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的一个控制器上实现一个基本控制器。在该基本控制器中,我想能够获得当前执行的ActionResult名称。



我将如何做这个?

  public class HomeController:ControllerBase 
{
public ActionResult Index()
{



和;

  public class ControllerBase:控制器
{
public ControllerBase()
{
//将获取执行ActionResult的方法
}
}


解决方案

由于控制器正在被实例化,你不能在控制器的构造函数中知道并且没有动作可以被调用。但是,您可以覆盖初始化方法并从路由引擎获取操作名称:

  protected override void Initialize(RequestContext requestContext)
{
base.Initialize(requestContext);
var actionName = requestContext.RouteData.Values [action];
}


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
    }
}

解决方案

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:

protected override void Initialize(RequestContext requestContext)
{
    base.Initialize(requestContext);
    var actionName = requestContext.RouteData.Values["action"];
}

这篇关于如何从基本控制器获取操作名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆