ASP.NET MVC - 控制器代码的当前操作? [英] ASP.NET MVC - Current Action from controller code?

查看:178
本文介绍了ASP.NET MVC - 控制器代码的当前操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与另一个最近的问题非常相似:



如何在ASP.NET MVC视图中返回当前操作?



但是,我想获取当前操作的名称



因此,在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屋!

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