从控制器内获取控制器和动作名称? [英] Get controller and action name from within controller?

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

问题描述

对于我们的 Web 应用程序,我需要根据视图保存获取和显示项目的顺序 - 或者更准确地说 - 生成视图的控制器和操作(当然还有用户 ID,但这不是重点在这里).

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).

我不只是在每个控制器动作中给自己一个标识符(为了将它用于一些依赖于视图的 DB 输出排序),我认为从控制器和动作自动创建这个标识符会更安全、更容易调用它的方法.

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?

推荐答案

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

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

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