This is very similar to another recent question:
How can I return the current action in an ASP.NET MVC view?
However, I want to get the name of the current action from within controller code.
So within the code of a function that's being called by an Action, I want to get a string of the name of the current Action.
Is this possible?
I can get to the current controller via ViewContext. Controller. GetType().
An action result is what a controller action returns in response to a browser request. The ASP.NET MVC framework supports several types of action results including: ViewResult - Represents HTML and markup. EmptyResult - Represents no result. RedirectResult - Represents a redirection to a new URL.
RequestContext. RouteData. Values["controller"]. ToString();
You can access the route data from within your controller class like this:
var actionName = ControllerContext.RouteData.GetRequiredString("action");
Or, if "action" isn't a required part of your route, you can just index into the route data as per usual.
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