繁体   English   中英

MVC问题Elmah

[英]MVC question Elmah

我正在做一个MVC项目,我从NerdDinner项目复制了很多工作。 在NerdDinner中,如果未找到晚餐,或者用户不是晚餐的所有者,我们将返回一些视图,例如DinnerNotFound,InvalidOwner。 但是在我的项目中,由于所有这些原因,想要创建一个视图(CustomException)并使用它。 因此,我引发了异常并在我的basecontrller的OnException事件中捕获了它们。 然后从那里我想在将其登录到ELMAH后呈现自定义视图。

但是渲染该视图的调用(RedirectToAction(“ CustomException”,ce);)似乎没有用,它确实导航到动作CustomException。

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

关于Parminder

ListingExceptions.cs

名称空间Listing.Exceptions {公共静态类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;

    }


}

}

BaseController.cs

命名空间Listing.Controllers {公共局部类BaseController:Controller {

    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 );
    }
}

}

ListingController.cs

命名空间Listing.Controllers

{

公共虚拟ActionResult详细信息(长ID,字符串标题){

        Item item = itemRepository.GetItemByID(id);

        if (item == null)

        {

            throw new Exception("ItemNotFound");

        }

        else

        {

            return View("Details", new ListingFormViewModel(item, photoRepository));

        }
    }

}

的global.asax.cs

route.MapRoute(“ Exception”,//路由名称“ {controller} / {action} / {ce}”,//具有新参数的URL {controller =“ Base”,action =“ CustomException”,ce =“”} );

我不确定您是否可以在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 );
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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