在交换机情况下未达到默认值

时间:2015-08-07 09:38:17

标签: c# asp.net-mvc

我有这个:

if (IPAddress.TryParse(item, out address))
{
    switch (address.AddressFamily)
    {
         case System.Net.Sockets.AddressFamily.InterNetwork:
             // we have IPv4
             break;
         case System.Net.Sockets.AddressFamily.InterNetworkV6:
             // we have IPv6
             break;                          
         default:
             new ValidationResult("A monkey must fling something");
             break;
    }
    //return new ValidationResult("A monkey must fling something");
}

但如果它不是IpAddress,则必须点击默认值:

new ValidationResult("A monkey must fling something");

但如果它不是IpAddress,它甚至会跳过该行?因此,如果它不是IpAddress,那么如何使它工作,它会触及默认语句。

谢谢

这是整个方法:

internal string Serialize(EditProductModel model)
{
    IpConditionRedirectValidation validation = new IpConditionRedirectValidation();
    XElement settings = XElement.Parse(model.Product.AuthenticationSettings ?? "<settings/>");
    XElement ipaddresses = new XElement("ipaddresses");

    if (IpAddress == null)
        return string.Empty;

    string[] lines = IpAddress.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    lines = IpAddress.Split(new char[0]);

    foreach (var item in lines)
    {
        if (item.StartsWith("127") || item.StartsWith("192"))


            return model.Product.AuthenticationSettings;

        IPAddress address;// = null;
        if (IPAddress.TryParse(item, out address))
        {

            switch (address.AddressFamily)
            {
                case System.Net.Sockets.AddressFamily.InterNetwork:
                    ipaddresses.Add(new XElement("ipaddress", item));
                    // we have IPv4
                    break;
                case System.Net.Sockets.AddressFamily.InterNetworkV6:
                    // we have IPv6
                    ipaddresses.Add(new XElement("ipaddress", item));
                    break;
                case System.Net.Sockets.AddressFamily.Unspecified:
                    break;
                default:
                    break;
            }
        }

    }
    settings.Add(ipaddresses);

    return settings.ToString();
}

2 个答案:

答案 0 :(得分:-1)

你实际上并没有对ValidationResult做任何事情,你只是new

当你传递的不是ip地址时,IPAddress.TryParse会失败,所以你永远不会进入你的转换。

答案 1 :(得分:-1)

我解决了,就像这样:

 foreach (var item in lines)
            {
                if (item.StartsWith("127") || item.StartsWith("192"))                
                    return new ValidationResult("IpAddress cant be saved");


                if (String.IsNullOrWhiteSpace(IpCondition.IpAddress))
                    return new ValidationResult(this.FormatErrorMessage(validationContext.DisplayName));

                IPAddress address;

                if (IPAddress.TryParse(item, out address))
                {
                    continue;
                }
                else
                {
                    return new ValidationResult("IpAddress cant be saved");
                }
            }

全部谢谢