简体   繁体   中英

Pass exception object to API parameter in .NET Core

I am working in Exception Logging, I have created API for that, API taking exception as parameter and few more thing.

        [HttpPost]
        [Route("/Log")]
        public IEnumerable<string> Post([FromBody] WP2Exceptions wp2Exceptions)
        {
            ExceptionsModel exceptionsModel = new ExceptionsModel();

                exceptionsModel = _exceptions.GetExceptionsByType(wp2Exceptions.exception.GetType().ToString());
                ExceptionsLogModel exceptionLogModel = new ExceptionsLogModel();
                exceptionLogModel.ExceptionID = exceptionsModel.ExceptionID;
                exceptionLogModel.ModuleName = System.Reflection.Assembly.GetEntryAssembly().GetName().Name;
                exceptionLogModel.ExceptionMessage = wp2Exceptions.exception.Message;
                exceptionLogModel.ExceptionType = wp2Exceptions.exception.GetType().ToString();
                exceptionLogModel.ExceptionSource = wp2Exceptions.exception.Source.ToString();
                exceptionLogModel.ExceptionUrl = wp2Exceptions.exception.StackTrace;
                _exceptionsLog.AddExceptionsLog(exceptionLogModel);

            return new string[] { exceptionsModel.ExceptionType, exceptionsModel.Message };
        }

public class WP2Exceptions
    {
        public string moduleName { get; set; }
        public Exception exception { get; set; }
    }

While i am passing exception in parameter i am getting "Bad Request" error

Test Code

public async void callAPI()
{
    HttpClient client = new HttpClient();
    client.BaseAddress = new Uri("http://localhost:50558/");
    try
    {
        string s = null;
        string sp = s.ToString();
    }
    catch (Exception ex)
    {
        var mydata = "{'exception':'" + JsonConvert.SerializeObject(ex)  + "','moduleName':'WEBAPI'}";
        var response = await client.PostAsync("Log", new StringContent(mydata, Encoding.UTF8, "application/json"));
        if (response != null)
        {
            Console.WriteLine("Log ID - " + response.ToString());
        }
    }
}

Please correct me where i am doing wrong or is it possible can we pass exception object as a WEB API parameter?

I resolve the problem,

In remove below code.

var mydata = "{'exception':'" + JsonConvert.SerializeObject(ex)  + "','moduleName':'WEBAPI'}";

Created new class and pass the data .

public class paramObject
{
    public string modulename { get; set; }
    public Exception exception { get; set; }
}

Inside callAPI method i implement following code.

pramObject po = new pramObject()
{
    modulename="Webapi",
    exception=ex,
};
var response = await client.PostAsync("Log", new StringContent(JsonConvert.SerializeObject(po), Encoding.UTF8, "application/json"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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