gpt4 book ai didi

c# - 在asp.net mvc3中执行的OnAction中读取属性

转载 作者:行者123 更新时间:2023-12-02 02:35:11 25 4
gpt4 key购买 nike

我有一个操作和属性,如下所示,我已覆盖 OnActionExecuting 并希望读取该方法中的属性

[MyAttribute(integer)]
public ActionResult MyAction()
{
}


protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
//here i want to read integer passed to action using Attribute
}

最佳答案

尝试一下:

Controller

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
foreach (var filter in filterContext.ActionDescriptor.GetCustomAttributes(typeof (MyAttribute), false).Cast<MyAttribute>())
{
var desiredValue = filter.Parameter;
}

base.OnActionExecuting(filterContext);
}

过滤

public class MyAttribute : FilterAttribute, IActionFilter
{
private readonly int _parameter;

public MyAttribute(int parameter)
{
_parameter = parameter;
}

public int Parameter { get { return _parameter; } }

public void OnActionExecuted(ActionExecutedContext filterContext)
{
//throw new NotImplementedException();
}

public void OnActionExecuting(ActionExecutingContext filterContext)
{
//throw new NotImplementedException();
}
}

关于c# - 在asp.net mvc3中执行的OnAction中读取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7134297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com