I'm building an ASP.NET MVC 5 app, and following the example in this SO post to apply [Flags]
to an enum
:
[Flags]
public enum Role
{
User = 1,
Finance = 2,
Management = 4,
SystemAdmin = 8
}
Note that, the [Flags]
attribute gives us a nice way to get multiple values as one, comma-separated string:
var roles = (Constants.Role.SystemAdmin | Constants.Role.Management).ToString();
// "SystemAdmin, Management"
The enum values will be used in the [Authorize]
attribute on controllers and actions, such as this:
[Authorize(Roles = (Constants.Role.SystemAdmin | Constants.Role.Management).ToString())]
public class SomeController : Controller
{ ... }
However, the above attribute generates this error:
An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type
Why can I use the .ToString()
method and get a CSV of the enums in code but not when using the same statement in the [Authorize]
attribute?