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"];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With