我在我的 MVC 代码中使用操作过滤器,我已将操作过滤器附加到下面提到的操作,但此操作是用 MVC 操作名称属性装饰的。
但我想要操作方法的原始名称(例如 ChangeOrder),但我将操作过滤器名称作为编辑。我不想删除 ActionName 属性。
[HttpPost, ActionName("Edit")]
[FormValueRequired("btnSaveOrderStatus")]
public ActionResult ChangeOrder(int id)
{
return View();
}
我应该在不删除 ActionName 属性的情况下获取 Actionmethod 原始名称吗? 请提供我如何在mvc中获取原始名称而不是修饰名称的方法。
最佳答案
您可以通过将 filterContext.ActionDescriptor
转换为 ReflectedActionDescriptor
来获取与 ActionName
对应的操作方法详细信息在过滤器中。该对象具有 MethodInfo
属性,它为您提供该方法的所有详细信息。
string actionMethodName = (filterContext.ActionDescriptor as ReflectedActionDescriptor)
.MethodInfo
.Name;
关于c# - 如果使用 ActionName MVC 属性装饰,则获取 Action 方法的原始名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39443541/