MVC 问题 Elmah
我正在开发一个 MVC 项目,我从 NerdDinner 项目中复制了很多工作。 在 NerdDinner 中,如果未找到晚餐或用户不是晚餐的所有者,我们将返回一些视图,例如 DiningNotFound、InvalidOwner。 但在我的项目中想要创建一个 view (CustomException) 并出于所有这些原因使用它。 因此,我引发异常并在我的 basecontrller 的 OnException 事件中捕获它们。 然后从那里我想在登录到 ELMAH 后渲染一个自定义视图。
但是调用渲染该视图 (RedirectToAction("CustomException",ce );) 似乎不起作用,它不会导航到 CustomException 操作。
有人可以帮我看看可能是什么原因吗? 我在这里列出了所有文件。 另外我应该如何进入 global.asax.cs 文件。 代码如下。
问候 Parminder
ListingExceptions.cs
命名空间 Listing.Exceptions { 公共静态类 ListingExceptions {
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
路线.MapRoute( "Exception", // 路由名称 "{controller}/{action}/{ce}", // 带参数的 URL 新{控制器=“基础”,操作=“CustomException”,ce=“”});
I am working on a MVC project where I have copied a lot of work from NerdDinner project.
In NerdDinner we are returning few views like DinnerNotFound, InvalidOwner if a dinner is not found or if a user is not an owner of the dinner. But In my project want to create an
view (CustomException) and use it for all such reasons. So I raise exception and catch them in my basecontrller's OnException event. Then from there I want to render a Custom view after loging it to ELMAH.
But the call to render that view (RedirectToAction("CustomException",ce );)
doesnt seem working, it doenst navigate to the action CustomException.
Can someone help me what could be the reason. I have listed all the files here.
Also how should I make an entry into global.asax.cs file.
codes are given below.
Regards
Parminder
ListingExceptions.cs
namespace Listing.Exceptions
{
public static class 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
namespace Listing.Controllers
{
public partial class 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
namespace Listing.Controllers
{
public virtual ActionResult Details(long id, string title)
{
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(
"Exception", // Route name
"{controller}/{action}/{ce}", // URL with parameters
new { controller = "Base", action = "CustomException", ce = "" } );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不太确定您可以像
OnException
中那样重定向。 添加filterContext.Result =
应该可以:I'm not so sure you can redirect like that within
OnException
. Adding afilterContext.Result =
should work: