从控制器内获取控制器和操作的名字吗? [英] Get controller and action name from within controller?

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

问题描述

有关我们的web应用程序,我需要保存获取的顺序,显示根据不同的视图中的项目 - 或者是precise - 生成视图控制器和动作(当然用户ID,但是这不是这里的点)。

而不是只给一个标识符自己在每个控制器动作(为了使用它为DB输出的一些看法依赖排序),我认为这将是更安全,更容易从控制器和动作自动创建这个标识符方法它被称为从

我怎样才能获得控制器和动作的名字从控制器中的操作方法中?或者我需要为反映?我想这是pretty容易,先谢谢了!


解决方案

 字符串actionName = this.ControllerContext.RouteData.Values​​ [行动]的ToString();
。字符串controllerName = this.ControllerContext.RouteData.Values​​ [控制器]的ToString();

For our web application I need to save the order of the fetched and displayed items depending on the view - or to be precise - the controller and action that generated the view (and the user id of course, but that's not the point here).

Instead of just giving an identifier myself in each controller action (in order to use it for some view-dependant sorting of DB outputs), I thought that it would be safer and easier to create this identifier automatically from the controller and action method it gets called from.

How can I get the name of the controller and action from within the action method in a controller? Or do I need reflection for that? I guess it's pretty easy, thanks in advance!

解决方案

string actionName = this.ControllerContext.RouteData.Values["action"].ToString();
string controllerName = this.ControllerContext.RouteData.Values["controller"].ToString();

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

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