Исключение не передается из API в приложение - PullRequest
0 голосов
/ 18 марта 2020

Я использую код веб-API MVC, получаю исключение на уровне базы данных, но когда он передается на прикладной уровень, он возвращается без каких-либо исключений.

Я не могу обработать исключение из веб-API

Уровень базы данных

public int DBPostGeneralConfig(GeneralConfigtDets Getgencon)
        {
            int result = 0;
            SqlConnection sqlConnection = null;
            try
            {
                string user = !string.IsNullOrEmpty(System.Environment.UserName) ? System.Environment.UserName : string.Empty;
                string sqlConnectString = MyConnectionString;
                sqlConnection = new SqlConnection(sqlConnectString);
                SqlCommand sqlCommand = new SqlCommand("InsertUpdateDeleGeneralConfig", sqlConnection);
                sqlCommand.CommandTimeout = MySQLCommandTimeOut;
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.Parameters.AddWithValue("@Action", "INSERT");
                sqlCommand.Parameters.AddWithValue("@ID", System.Guid.NewGuid().ToString());
                sqlCommand.Parameters.AddWithValue("@Name", Getgencon.Name);
                sqlCommand.Parameters.AddWithValue("@Value", Getgencon.Value);
                sqlCommand.Parameters.AddWithValue("@updatedBy", user);
                sqlCommand.Parameters.AddWithValue("@updatedOn", DateTime.Now);
                sqlConnection.Open();
                result = sqlCommand.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                DBCall.LogError(ex, "DBPostGeneralConfig");
                result = -1;
            }

            finally
            {
                if (sqlConnection.State != ConnectionState.Closed)
                {
                    sqlConnection.Close();
                }
            }
            return result;
        }

Уровень приложения

  [HttpPost]
  [ValidateAntiForgeryToken]

      public ActionResult AddNewGeneralConfig(GeneralConfigDets Configval)
                {
                    if (ModelState.IsValid)
                    {
                        try
                        {
                            ServiceRepositary serviceObj = new ServiceRepositary();
                            HttpResponseMessage response = serviceObj.PostResponse("api/GeneralConfiguration/AddNewGeneralConfig", Configval);
                            response.EnsureSuccessStatusCode();

                        }
                        catch (Exception)
                        {
                            ModelState.AddModelError(string.Empty, "Server error. Please contact administrator.");
                        }

                        TempData["Message"] = "Added Successfully";
                    }
                    return RedirectToAction("GetGeneralConfig", "GeneralConfig");
                }
...