This used used to work with earlier version of .Net. What's the equivalent in .net core terms. Now I get following error:
'ActionDescriptor' does not contain a definition for 'GetCustomAttributes' and no extension method 'GetCustomAttributes' accepting a first argument of type 'ActionDescriptor' could be found
public virtual void SetupMetadata(ActionExecutingContext filterContext)
{
var myAttr = filterContext.ActionDescriptor.GetCustomAttributes(typeof(MyAttribute), false);
if (myAttr.Length == 1)
//do something
}
Attribute definition:
public class MyAttribute : Attribute
{
private readonly string _parameter;
public PageTitleAttribute(string parameter)
{
_parameter = parameter;
}
public string Parameter { get { return _parameter; } }
}
Code Usage:
[MyAttribute("Attribute value is set here")]
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
For ASP.NET Core 3+:
var filters = context.Filters;
// And filter it like this:
var filtered = filters.OfType<OurFilterType>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With