MVC 问题 Elmah

发布于 2024-07-27 20:03:53 字数 2618 浏览 4 评论 0原文

我正在开发一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀子花开つ 2024-08-03 20:03:53

我不太确定您可以像 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 );
}

I'm not so sure you can redirect like that within OnException. Adding a filterContext.Result = should work:

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 );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文