I have the following code block: –
using System.ComponentModel.DataAnnotations;
public class NewsItem
{
[RegularExpression(System.Configuration.ConfigurationSettings.AppSettings["UrlRegEx"], ErrorMessage = "Invalid link")]
public string Url { get; set; }
}
This returns the error “An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type”.
How can I give the value in the first parameter of a RegularExpression attribute. I want to give the values from Web.Config variables. I am using this code in the Model class of EF.
You can point data annotation to pick the value from resource file
See ErrorMessageResourceName
OR
You can write your custom validation method and pick the value from there
See CustomValidationAttribute
OR
You can write a regular expression derived attribute class that can reads the value from web.config.