Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为localhost验证C#中的URL_C#_Asp.net Mvc - Fatal编程技术网

如何为localhost验证C#中的URL

如何为localhost验证C#中的URL,c#,asp.net-mvc,C#,Asp.net Mvc,我正在MVC中使用UrlAttribute 但它不接受本地主机URL,例如 这将验证URL,但不验证本地主机URL 我怎样才能做到这一点?您可以使用127.0.0.1:portNumber。也许这会有所帮助。关于您的问题,您有3个选择: 使用IP地址而不是本地主机 使用有效的url并在宿主文件中插入条目 创建新的URLAttribute扩展并忽略其中的Localhost 如果我没弄错,您可以使用自定义ValidationAttribute。在模型命名空间中添加此类 public class Ur

我正在MVC中使用UrlAttribute

但它不接受本地主机URL,例如

这将验证URL,但不验证本地主机URL

我怎样才能做到这一点?

您可以使用127.0.0.1:portNumber。也许这会有所帮助。

关于您的问题,您有3个选择:

  • 使用IP地址而不是本地主机
  • 使用有效的url并在宿主文件中插入条目
  • 创建新的URLAttribute扩展并忽略其中的Localhost

  • 如果我没弄错,您可以使用自定义ValidationAttribute。在模型命名空间中添加此类

    public class UrlValidator : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (value != null)
            {
                var x = value.ToString();
                if (Regex.IsMatch(x, @"^http:\/\/\w+(\.\w+)*(:[0-9]+)?\/?(\/[.\w]*)*$", RegexOptions.IgnoreCase))
                {
                    return ValidationResult.Success;
                }
                else
                {
                    return new ValidationResult(ErrorMessage);
                }
            }
            else
            {
                return new ValidationResult("Please enter some value");
            }
        }
    }
    
    像这样使用它

    [UrlValidator(ErrorMessage = "please_enter_valid_ftp_url")] 
    public string Url { get; set; }
    
    当然,您可以修改正则表达式以满足您的需求。我在这个特别的例子中使用的这个方法对于像

    http://example
    http://example.com
    http://127.0.0.1
    

    我还建议创建一个自定义验证器属性类。但我想使用System.Uri类来验证,而不是使用自定义的个人正则表达式

    public class UriAttribute: ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            Uri uri;
            bool valid = Uri.TryCreate(Convert.ToString(value), UriKind.Absolute, out uri);
    
            if (!valid)
            {
                return new ValidationResult(ErrorMessage);
            }
            return ValidationResult.Success;
        }
    }
    

    通过使用System.Uri类,我们可以为自己的正则表达式排除出错的机会。

    为本地主机(如mysampleapp.com)分配一个域名。您可以通过将本地主机IP映射到hosts文件中的此应用程序名称来完成此操作。或者看这里-源代码在这里:似乎您只需要匹配给定的正则表达式-这是一个doozyYou有
    ErrorMessage=“请输入有效的ftp\u url”
    和,
    ErrorMessage=null
    ,我认为需要删除带有
    null
    的。
    public class UriAttribute: ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            Uri uri;
            bool valid = Uri.TryCreate(Convert.ToString(value), UriKind.Absolute, out uri);
    
            if (!valid)
            {
                return new ValidationResult(ErrorMessage);
            }
            return ValidationResult.Success;
        }
    }