How to serialize custom exception for json in c #

I have a C # web service that I call using jquery ajax. It works fine, except when a custom exception is thrown inside a web method. For some reason, the responseSext object of XmlHttpResponse objects has only the basic properties of the Exception class. This way I get a json object with the following properties: "ExceptionType", "Message" and "StackTrace"

My custom exception has a property called "FieldErrors" that does not appear in the return. Here is the code for this class:

[Serializable]
[XmlRootAttribute(Namespace = "http://www.mydomain.com/", IsNullable = false)]
public class ValidationException : Exception
{
    public List<string> FieldErrors { get; set; }
    public ValidationException(string message = null, Exception innerException = null) : base(message: message, innerException: innerException) 
    {
        this.FieldErrors = new List<string>();
    }
}

My goal is to get the "FieldErrors" property to display in the json response.

+3
1

. asp.net.

, .

, , json- - , .

+3

Source: https://habr.com/ru/post/1765881/


All Articles