WebApi, I can not serialize declared attributes in a class derived from Exception

0

I can not get the values of the properties declared in BaseException when I call the Get method of the API.

public class BaseException : Exception
{
    public  string ExType { get; set; }

    public JObject Properties { get; set; }

    public Guid ErrorCodeId { get; set; }

    public BaseException(string message): base(message) { }
}

public class BadRequestException : BaseException
{
    public BadRequestException(string message) : base(message) { }
}

// GET: api/<controller>
public virtual IHttpActionResult Get()
{
    IHttpActionResult result = null;
    try
    {
        throw new Exception("Error description here");
        result = Ok();
    }
    catch (Exception ex)
    {
        result = ResponseMessage(Request.CreateResponse(HttpStatusCode.BadRequest, new BadRequestException(ex.Message)
        {
            ExType = "Any exception type"//Can't get this value in the output JSON
        }));
    }
    return result;
}

The ExType property is not showing. The result is the following:

{
  "ClassName": "BadRequestException",
  "Message": "Error description here",
  "Data": null,
  "InnerException": null,
  "HelpURL": null,
  "StackTraceString": null,
  "RemoteStackTraceString": null,
  "RemoteStackIndex": 0,
  "ExceptionMethod": null,
  "HResult": -2146233088,
  "Source": null,
  "WatsonBuckets": null
}

Is there any way to get the serialized value of my own properties?

    
asked by Guillermo Subiran 29.05.2017 в 13:00
source

1 answer

0

You can try this:

[Serializable()]
    public class BaseException : Exception
    {
        public string ExType { get; set; }

        public JObject Properties { get; set; }

        public Guid ErrorCodeId { get; set; }

        public BaseException(string message) : base(message) { }
    }

Generically, otherwise you can mark the tags of each of the attributes.

Here is some information

Controlling XML Serialization Using Attributes

I hope it serves you.

    
answered by 29.05.2017 в 13:20