就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the help center为指导。
9年前关闭。
我正在寻找一种标准方法来处理 asp.net mvc 2.0 或 3.0 中的错误
最佳答案
对于 Controller 范围错误,请尝试使用自定义 Exception 属性,即
public class RedirectOnErrorAttribute : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
// Don't interfere if the exception is already handled
if(filterContext.ExceptionHandled)
return;
//.. log exception and do appropriate redirects here
}
}
然后用属性装饰 Controller ,错误处理应该是你的
[RedirectOnError]
public class TestController : Controller
{
//.. Actions etc...
}
如果错误与路由有关,则无济于事 - 即它首先找不到 Controller 。为此,请尝试 Global.asax 中的应用程序错误处理程序,即
protected void Application_Error(object sender, EventArgs e)
{
//.. perhaps direct to a custom error page is here
}
我不知道这是否是“最佳实践”。行得通。
关于asp.net-mvc - asp.net mvc 错误处理的最佳实践,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4523831/