我想将动态变量作为参数传递给我的属性.在这里,我想使用Environment.MachineName,请参阅下面的代码:
public interface IMonitoringViewModelConfiguration : IConfigurationContainer
{
[ConfigurationKey("MonitoringService", Environment.MachineName)]
string ConnectionString { get; }
}
Run Code Online (Sandbox Code Playgroud)
但我收到此错误:错误1属性参数必须是属性参数类型Abc.ServiceBus.Monitoring.ViewModel的常量表达式,typeof表达式或数组创建表达式
为了传递我的Environment.MachineName,是否有尽可能干净的解决方法?
谢谢.
约翰
PS:我发现了一些谈论这个案子的文章,但它已经写成2 - 3年前了.但今天,来自.NET 4.0的clr能提供一些不错的解决方案吗?
我有以下代码:
[Required(ErrorMessage = MessageModel.translateMessage("required")))]
[Display(Name= MessageModel.translateMessage("id"))]
public string user_id { get; set; }
Run Code Online (Sandbox Code Playgroud)
我试图使错误消息动态,但我在编译时得到错误:
"An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type."
解决这个问题的任何方法?
我正在尝试为我的控制器设置授权可以执行以下操作:
[Authorize(Roles = "Approver")]
Run Code Online (Sandbox Code Playgroud)
角色的名称如何保存在数据库中,因此我想尝试执行以下操作:
[Authorize(Roles = Settings.Instance.RoleEmployee)]
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误:
属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式
有什么办法解决这个问题吗?