I want to declare a class to search some topics with specific key. First, I've declared an interface ISearch
interface ISearch
{
[Required(ErrorMessage = "Search key must be required.")]
string Key { get; set; }
Task<IEnumerable<TopicViewModels>> Search();
}
Then, I want to check Key
is null or not via using RequiredAttribute
.
My question: How to get the error message to throw to user if Key
is null?
I don't want to use this way:
Task<IEnumerable<TopicViewModels>> Search(string key)
{
if (!string.IsNullOrEmpty(key))
{
// start searching....
}
// throw error message
}