如何在MS ASP.NET MVC中重定向到视图中的错误页面?

时间:2022-05-27 17:07:07

In Controller, RedirectToAction("Error") can redirect to error page. How to write code in view code to allow the page redirected to error.aspx view?

在Controller中,RedirectToAction(“Error”)可以重定向到错误页面。如何在视图代码中编写代码以允许页面重定向到error.aspx视图?

2 个解决方案

#1


You shouldn't need to handle reporting of errors inside MVC actions, and hard coding error behaviour makes finding a fixing issues harder than it needs to be.

您不应该需要处理MVC操作中的错误报告,并且硬编码错误行为会使找到修复问题变得比它需要的更难。

Instead use the HandleError attribute and throw a regular exception:

而是使用HandleError属性并抛出常规异常:

[HandleError]
public class ThingController : Controller 
{
    public ActionResult DoStuff()
    {
        ...

        // uh-oh! throw exception
        throw new WhateverException("message");
    }
}

This will allow you to use the customErrors config flag to change your application's behaviour:

这将允许您使用customErrors配置标志来更改应用程序的行为:

<customErrors mode="RemoteOnly" defaultRedirect="~/System/Error" />

Then locally you'll get an ugly but detailed yellow screen of death, plus debug will break on the thrown exception by default.

然后在本地你将获得一个丑陋但详细的黄色死亡屏幕,加上调试将默认打破抛出的异常。

Remote users will get redirected to your SystemController.Error where you can have a nice user-friendly message and log the error.

远程用户将被重定向到您的SystemController.Error,您可以在其中获得一个友好的用户友好消息并记录错误。

This will work for controller actions and views in the same way.

这将以相同的方式用于控制器操作和视图。

#2


@Jørn is right, but what kinds of errors are you predicting to show up in your view?

@Jørn是对的,但是你预测会在你的视图中出现什么样的错误?

If you gave some details on why you're thinking about this, we might be able to suggest some better alternatives instead of simply saying "don't do this."

如果你详细说明了为什么要考虑这个问题,我们可能会建议一些更好的选择,而不是简单地说“不要这样做”。

#1


You shouldn't need to handle reporting of errors inside MVC actions, and hard coding error behaviour makes finding a fixing issues harder than it needs to be.

您不应该需要处理MVC操作中的错误报告,并且硬编码错误行为会使找到修复问题变得比它需要的更难。

Instead use the HandleError attribute and throw a regular exception:

而是使用HandleError属性并抛出常规异常:

[HandleError]
public class ThingController : Controller 
{
    public ActionResult DoStuff()
    {
        ...

        // uh-oh! throw exception
        throw new WhateverException("message");
    }
}

This will allow you to use the customErrors config flag to change your application's behaviour:

这将允许您使用customErrors配置标志来更改应用程序的行为:

<customErrors mode="RemoteOnly" defaultRedirect="~/System/Error" />

Then locally you'll get an ugly but detailed yellow screen of death, plus debug will break on the thrown exception by default.

然后在本地你将获得一个丑陋但详细的黄色死亡屏幕,加上调试将默认打破抛出的异常。

Remote users will get redirected to your SystemController.Error where you can have a nice user-friendly message and log the error.

远程用户将被重定向到您的SystemController.Error,您可以在其中获得一个友好的用户友好消息并记录错误。

This will work for controller actions and views in the same way.

这将以相同的方式用于控制器操作和视图。

#2


@Jørn is right, but what kinds of errors are you predicting to show up in your view?

@Jørn是对的,但是你预测会在你的视图中出现什么样的错误?

If you gave some details on why you're thinking about this, we might be able to suggest some better alternatives instead of simply saying "don't do this."

如果你详细说明了为什么要考虑这个问题,我们可能会建议一些更好的选择,而不是简单地说“不要这样做”。