ASP.NET MVC - 控制器代码的当前操作? [英] ASP.NET MVC - Current Action from controller code?
本文介绍了ASP.NET MVC - 控制器代码的当前操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这与另一个最近的问题非常相似:
This is very similar to another recent question:
但是,我想从 controller 代码中获取当前操作的名称.
However, I want to get the name of the current action from within controller code.
因此,在由 Action 调用的函数代码中,我想获取当前 Action 名称的字符串.
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.
这可能吗?
推荐答案
您可以像这样从控制器类中访问路由数据:
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屋!
查看全文