ASP.NET MVC - 控制器代码的当前操作? [英] ASP.NET MVC - Current Action from controller code?
问题描述
这与另一个最近的问题非常相似:
但是,我想获取当前操作的名称
因此,在Action调用的函数的代码中,我想获取一个字符串
这是可能的吗?您可以从控制器类中访问路由数据,如下所示:
var actionName = ControllerContext.RouteData.GetRequiredString(action); / code>
或者,如果action不是路由的必需部分,则可以按照常规索引到路由数据。 / p>
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?
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.
这篇关于ASP.NET MVC - 控制器代码的当前操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!