gpt4 book ai didi

asp.net-mvc-4 - 属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式。

转载 作者:行者123 更新时间:2023-12-04 06:09:07 24 4
gpt4 key购买 nike

我想将组名分配为授权过滤器的属性。

它将如下

[FilterConfig.AuthorizeAd(Group = "DirectoryName")]
public ActionResult GetData()
{
}


Insted硬编码我尝试通过添加如下

[FilterConfig.AuthorizeAd(Group = Constants.ActiveDirectoryName)]

Where Constants is class and created member as below:

public const  string ActiveDirectoryName = "directoryName";


现在我想从 app.config 访问它,尝试如下

[FilterConfig.AuthorizeAd(Group = ConfigurationManager.AppSettings["Directory_Name"].ToString()


它通过错误消息作为“属性参数必须是常量表达式”

如何从配置中分配数据?
请建议我。

最佳答案

您不能使用属性来做到这一点,它们必须是错误消息中所述的常量。如果你想从配置文件中获取一个值,你可以通过将键传递给属性来实现,然后在构造函数中从配置管理器中获取你想要的值

    public MyAttribute :Attribute
{
private string _config;
public MyAttribute(string configKey)
{
_config = ConfigurationManager.AppSettings[configKey];

...
}
}

HTH

关于asp.net-mvc-4 - 属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31481820/

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