【问题标题】:How I can validate input field in Webservice in Asp.net C#?如何在 Asp.net C# 中验证 Webservice 中的输入字段?
【发布时间】:2019-10-08 11:52:12
【问题描述】:

我正在用 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,我正在为第三方重量属性分配重量,但在第三方他们只接受 50 公斤,所以在分配时我需要在网络服务中检查重量我该怎么做?

【问题讨论】:

  • 可能与此重复:stackoverflow.com/questions/40595148/…,如果您知道限制,这将起作用。如果您不知道某个属性的最大值是多少,那么 Web 服务需要发送一个响应让您知道这一点。
  • 我的回答解决了你的问题吗?
  • 你的答案是正确的,但我用另一种方式做到了。

标签: c# asp.net rest web-services soap


【解决方案1】:

您可以通过以下解决方法验证您的属性:

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;

}

【讨论】:

    猜你喜欢
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多