如何在单个方法中处理异常而不是在mvc2(.net)中的每个方法中处理

时间:2012-10-30 06:51:21

标签: asp.net-mvc-2

我可以在整个页面或整个控制器的单一方法中处理异常。

这是我的代码

    public ActionResult BindProductDetails(string StartIndex, string EndIndex, string HeaderName, string SortBy, string Search, string OrderstoDisplay)
    {
        List<Products> objProduct = new List<Products>();

        try
        {
            objProduct = objProductsModel.GetProductsList(Convert.ToInt32(Session["MerchantID"]), Convert.ToInt32(StartIndex), Convert.ToInt32(EndIndex), HeaderName.Trim(), SortBy, Search, Convert.ToInt32(Session["userID"]), Convert.ToInt32(OrderstoDisplay), Session["ConnectionString"].ToString());
            ViewData["ProductCount"] = ProductsModel.ProductCount;
            ViewData["OrdersToDisplay"] = ProductsModel.OrdersToDisplay;
            CurrentProductCount = ProductsModel.CurrentCount;
            return PartialView("PVProducts", objProduct);
        }
        catch (Exception ex)
        {
           Logger.WriteToLog(objUserDetails.ErrorType, objUserDetails.MerchantName, objUserDetails.UserName, "ProductsController.BindProductDetails()", ex.Message, null, objProduct);
            return null;
        }
    }

    #region GetWarehouseDetails
    public string GetWarehouseDetails()
    {
        string strMerchantWarehouses = string.Empty;
        ProductDetails objUsersWarehouse = new ProductDetails();
        try
        {
            objUsersWarehouse = objProductsModel.GetWarehouseInfo(Convert.ToInt32(Session["MerchantID"]), Session["ConnectionString"].ToString());
            strMerchantWarehouses = json.toJshon(objUsersWarehouse);
        }
        catch (Exception ex)
        {
           Logger.WriteToLog(objUserDetails.ErrorType, objUserDetails.MerchantName, objUserDetails.UserName, "ProductsController.GetWarehouseDetails()", ex.Message, null, objUsersWarehouse);
        }
        return strMerchantWarehouses;
    }
    #endregion

每次我在每种方法中处理异常。我需要替代方法,

单个方法处理单个控制器或整个应用程序异常,如(Actionfilter)用于(会话处理)

1 个答案:

答案 0 :(得分:0)

您应该在Global.asax.cs中覆盖Applicaion_Error,将代码置于此方法