0

I'm writing a Web service in Asp.net C#, i need to validate field in web service itself, how i can do that? i have given small example below

 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;

}

Here List coming from third party api, I'm assigning weight to third party weight property but in third party they only accept 50kg, so while assigning i need to check weight in web service how i can do that?

3
  • Potential duplicate with this: stackoverflow.com/questions/40595148/… , this would work if you know the limitations. If you don't know what the maximum value is for a property then the web service needs to send a response letting you know that. Commented May 22, 2019 at 4:23
  • Did my answer solve your problem? Commented May 26, 2019 at 4:46
  • your answer is correct, but I did in another way. Commented May 28, 2019 at 4:45

1 Answer 1

1

You can validate your properties like the workaround below:

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;

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.