这曾经用于早期版本的 .Net。 .net 核心术语中的等价物是什么。现在我收到以下错误:
“ActionDescriptor”不包含“GetCustomAttributes”的定义,并且找不到接受“ActionDescriptor”类型的第一个参数的扩展方法“GetCustomAttributes”
public virtual void SetupMetadata(ActionExecutingContext filterContext)
{
var myAttr = filterContext.ActionDescriptor.GetCustomAttributes(typeof(MyAttribute), false);
if (myAttr.Length == 1)
//do something
}
属性定义:
public class MyAttribute : Attribute
{
private readonly string _parameter;
public PageTitleAttribute(string parameter)
{
_parameter = parameter;
}
public string Parameter { get { return _parameter; } }
}
代码用法:
[MyAttribute("Attribute value is set here")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
最佳答案
希望能帮助到别人,我是这样做的:
var attrib = (filterContext.ActionDescriptor as ControllerActionDescriptor).MethodInfo.GetCustomAttributes<MyAttribute>().FirstOrDefault();
关于c# - 通过 Controller .Net Core 中的 ActionExecutingContext 获取自定义属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41803449/