从类方法到Web api调用获取错误的最佳方法

时间:2019-06-09 15:17:11

标签: c# asp.net-web-api

我有一个可以正确插入数据的类,但是该函数正在与webapi通信。

如何将异常信息从共享库返回到Web api?

public void AddStockTransfer(StockTransfer item)
{
  using (var connection = new SqlConnection(ConfigurationManager.AppSettings["DataConnectionLive"]))
  {
   connection.Open();
   string insertQuery = @"
INSERT INTO[fuel].[StockTransfer]
        ([StockTransferGuid]
          ,[StockItemId]
          ,[Quantity]
          ,[SourceWarehouseId]
          ,[SourceBin]
          ,[TargetWarehouseId]
          ,[TargetBin]
          ,[DateTimeCreated]
          ,[DateTimeLastUpdated]
          ,[StockTransferStatus]
          ,[StatusMessage])
    VALUES (@StockTransferGuid,@StockItemId,@Quantity,@SourceWarehouseId,@SourceBin,@TargetWarehouseId,@TargetBin,@DateTimeCreated,@DateTimeLastUpdated,@StockTransferStatus,@StatusMessage)";

                    var mydate = DateTime.Now;
                    var mydate2 = DateTime.Now;
                    var result = connection.Execute(insertQuery, new
                    {
                        item.StockTransferGuid,
                        item.StockItemId,
                        item.Quantity,
                        item.SourceWarehouseId,
                        item.SourceBin,
                        item.TargetWarehouseId,
                        item.TargetBin,
                        mydate,
                        mydate2,
                        item.StockTransferStatus,
                        item.StatusMessage
                    });



            }catch (Exception ex)

            {
                    logger.Warn("Connection string is " + connection.ConnectionString.ToString());
                logger.Warn("Error occoured on add stock transfer funciton " + ex.Message.ToString());
            }


        }
    }

我想传递记录器看到的内容,并将其传递给我的Web方法。

[HttpPost]
public ActionResult Create(StockTransfer stockTrans)
{
  try
        {

            database.AddStockTransfer(stockTrans);
        }
        catch (Exception ex)
        {

            logger.Warn("Error on Create ex");
        }



        return Json(true);
  }

0 个答案:

没有答案