Search code examples
c#classvalidationwebaddress

web url validator to class level validation


I have a simple class and corresponding properties. I would like to add some validation rules (to a web property). I want to check that the web address is valid. What are the types of conditions I would need to check for?

My thinking: I have to require this format www.example.sometext

Should I check for valid .sometext endings (i.e. .com, .org, etc.) Problem is there are basically unlimited of these now.

So my question who would I add a validation rule on the class level to handle this?

public string WebAddress { get; set; }

Solution

  • You can use Uri.IsWellFormedUriString method. It will return true if the string was well-formed, if not it will return false.

    Link to MSDN: IsWellFormedUriString