Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type

I want to assign Group name as an attribute for authorize filter.

It will take as below

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

Insted of hardcoding i tried by adding it as below

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

Where Constants is class and created member as below:

public const  string ActiveDirectoryName = "directoryName";

Now i want to aceess it from app.config, tried as below

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

It is throughing the error msg as "An attribute argument must be a constant expression"

How can i assign the data from config? Please suggest me.

like image 962
user1893874 Avatar asked Jul 17 '15 17:07

user1893874


1 Answers

You can't do that with attributes, they have to be constants as stated in the error message. If you wanted to get a value from the configuration file, you could do it by passing the key to the attribute, and then in the constructor get the value you want from the configurationmanager

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

            ...
        }
    }

HTH

like image 159
Slicksim Avatar answered Nov 18 '22 02:11

Slicksim