【问题标题】:Get Action Method original name if its decorate with ActionName MVC attribute如果使用 ActionName MVC 属性装饰,则获取操作方法的原始名称
【发布时间】:2016-09-12 04:53:14
【问题描述】:

我在我的 MVC 代码中使用动作过滤器,我附加了带有下面提到的动作的动作过滤器,但是这个动作是用 MVC 动作名称属性装饰的。

但我想要操作方法的原始名称(例如 ChangeOrder),但我输入了操作过滤器名称作为编辑。我不想删除 ActionName 属性。

[HttpPost, ActionName("Edit")]
[FormValueRequired("btnSaveOrderStatus")]
public ActionResult ChangeOrder(int id)
{
    return View();
}

我是否应该在不删除 ActionName 属性的情况下获得 Actionmethod 原始名称。 请提供我如何在 mvc 中获取原始名称而不是修饰名称的方法。

【问题讨论】:

  • 为什么不到处使用 Edit(或 ChangeOrder)?
  • @Div 我们从编辑页面收集表单。请再次查看问题,我已更新。
  • @vatsal,所以,你在filterContext.ActionDescriptor.ActionName 中得到Edit

标签: c# asp.net-mvc


【解决方案1】:

您可以通过将filterContext.ActionDescriptor 转换为过滤器中的ReflectedActionDescriptor 来获取与ActionName 对应的操作方法详细信息。该对象具有MethodInfo 属性,它为您提供该方法的所有详细信息。

string actionMethodName = (filterContext.ActionDescriptor as ReflectedActionDescriptor)
            .MethodInfo
            .Name;

【讨论】:

  • @vatsal 很高兴为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 2019-12-21
  • 2014-12-25
  • 1970-01-01
  • 2011-03-15
  • 2023-02-16
  • 2021-05-31
相关资源
最近更新 更多