繁体   English   中英

如何在Asp.net C#中验证Web服务中的输入字段?

[英]How I can validate input field in Webservice in Asp.net C#?

我正在用Asp.net C#编写Web服务,我需要验证Web服务本身中的字段,我该怎么做? 我在下面给出了一个小例子

 public class Pack{
    public double Weight { get; set; }
    public double Height { get; set; }  
}

[WebMethod]
public string CreateShip(Pack pk){

  List<Ship> Sh = new List<Ship>();
  sh.weight=pk.Weight;

}

在这里,列表来自第三方api,我为第三方的weight属性分配了权重,但是在第三方中,他们仅接受50kg,所以在分配时我需要检查Web服务中的权重,我该怎么做?

您可以验证您的属性,例如以下解决方法:

public class Pack {
    private double _weight;
    public double Weight {
        get = >_weight;
        set {
            if (_weight > 50) throw new Exception("Weight is limited up to 50k.");
            _weight = value;
        }
    }
    public double Height {
        get;
        set;
    }
}

[WebMethod]
public string CreateShip(Pack pk) {

    List < Ship > Sh = new List < Ship > ();
    sh.weight = pk.Weight;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM