代码之家  ›  专栏  ›  技术社区  ›  Parminder

MVC问题Elmah

  •  2
  • Parminder  · 技术社区  · 15 年前

    我正在一个MVC项目中工作,我从Nerddinner项目中复制了很多工作。 在nerddinner中,我们返回的视图很少,如dinner not found、invalidowner(如果找不到晚餐或用户不是晚餐的所有者)。但在我的项目中要创建一个 查看(customException)并出于所有这些原因使用它。所以我提出异常并在BaseContrller的OnException事件中捕获它们。然后在登录到Elmah之后,我想从那里呈现一个自定义视图。

    但呈现该视图的调用(RedirectToAction(“CustomException”,CE);) 似乎不起作用,它无法导航到操作customerexception。

    有人能帮我什么原因吗?我在这里列出了所有的文件。 另外,我应该如何进入global.asax.cs文件。 代码如下。

    当做 帕梅德

    列出exceptions.cs

    命名空间列表。异常 { 公共静态类listingexeceptions {

        public static CustomException GetCustomException(Exception ex)
        {
            CustomException ce = new CustomException();
            switch (ex.Message)
            {
                case ItemConstant.INVALID_OWNER:
                    ce = new CustomException("Invalid Owner", "OOps you are not owner of this item");
                    break;
                case ItemConstant.ITEM_NOT_FOUND:
                    ce = new CustomException("Item not Found", "The Item you are looking for couldnt be found");
                    break;
                default:
                   ce = new CustomException("Error ", "There is an Error.");
                   break;
            }
    
            return ce;
    
        }
    
    
    }
    

    }

    基极控制器.cs

    命名空间列表。控制器 { 公共分部类BaseController:控制器 {

        public virtual ActionResult CustomException(CustomException ce)
        {
            return View(ce);   
        }
    
        protected override void OnException(ExceptionContext filterContext)
        {
            base.OnException(filterContext);
    
            CustomException ce = ListingExeceptions.GetCustomException(filterContext.Exception);
            ErrorSignal.FromCurrentContext().Raise(filterContext.Exception);
            filterContext.ExceptionHandled = true;
    
            RedirectToAction("CustomException",ce );
        }
    }
    

    }

    列出controller.cs

    命名空间列表。控制器

    {

    公共虚拟操作结果详细信息(长ID、字符串标题) {

            Item item = itemRepository.GetItemByID(id);
    
            if (item == null)
    
            {
    
                throw new Exception("ItemNotFound");
    
            }
    
            else
    
            {
    
                return View("Details", new ListingFormViewModel(item, photoRepository));
    
            }
        }
    

    }

    全球ASC.CS

    路线.maproute( “异常”,//route name “控制器/操作/ce”,//带参数的URL new controller=“base”,action=“customException”,ce=“”);

    1 回复  |  直到 15 年前
        1
  •  1
  •   eyesnz    15 年前

    我不太确定你能在 OnException . 添加一个 filterContext.Result = 应该工作:

    protected override void OnException(ExceptionContext filterContext)
    {
        base.OnException(filterContext);
    
        CustomException ce = ListingExeceptions.GetCustomException(filterContext.Exception);
        ErrorSignal.FromCurrentContext().Raise(filterContext.Exception);
        filterContext.ExceptionHandled = true;
    
        filterContext.Result = RedirectToAction("CustomException",ce );
    }