题
我的工作,我已经复制从项目的NerdDinner很多工作的MVC项目。 在NerdDinner范例,我们或者如果用户不是晚餐的所有者返回像DinnerNotFound,InvalidOwner一些看法,如果晚饭没有找到。但是,在我的项目要创建 视图(CustomException),并用它为所有这些原因。所以,我提出的例外,赶上他们在我的basecontrller的onException的事件。然后从那里我想使其登录电子以ELMAH后自定义视图。
但是,为了呈现该视图的呼叫(RedirectToAction( “CustomException”,CE);) 似乎没有工作,它doenst导航到动作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:控制器 {
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
routes.MapRoute( “异常”,//路线名称 “{控制器} / {行动} / {CE}”,// URL与参数 新{控制器= “基地”,动作= “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 );
}