示例#1
0
 public IEnumerable <ModelValidationResult> Validate(ModelValidationContext context)
 {
     if (NotAllowed.Contains(context.Model as string))
     {
         return(new List <ModelValidationResult> {
             new ModelValidationResult("", ErrorMessage)
         });
     }
     else
     {
         return(Enumerable.Empty <ModelValidationResult>());
     }
 }
示例#2
0
        public string ShortenWithCustomHash(string url, string customHash)
        {
            if (String.IsNullOrWhiteSpace(customHash))
            {
                throw new ArgumentException("The custom short URL cannot be empty.");
            }
            if (customHash.Length > 100)
            {
                throw new ArgumentException("The custom short URL must be no longer than 100 characters.");
            }
            var unreserved = new Regex(@"[a-z]|[A-Z]|[0-9]|\-|\.|_|\~");

            if (customHash.EndsWith(".") || !customHash.All(c => unreserved.IsMatch(c.ToString())))
            {
                throw new ArgumentException(
                          "The custom short URL must only contain letters A ... Z, numbers 0 ... 9 or " +
                          "dash (-), underscore (_), dot(.), or tilde (~)");
            }
            if (NotAllowed.Contains(customHash))
            {
                throw new ArgumentException("That custom short URL is not available.");
            }
            var directUrl = UrlUnwrapper.GetDirectUrl(url);

            if (Repository.ContainsKey(customHash))
            {
                throw new ArgumentException("The given custom short URL is already in use.");
            }
            if (Repository.ContainsValue(directUrl))
            {
                var oldKey = Repository.GetKey(directUrl);
                Repository.Remove(oldKey);
            }
            Repository.Add(customHash, directUrl);
            return(CompleteUrl(customHash));
        }