non*_*.cs 14 error-handling asp.net-mvc
我正在寻找一种标准方法来处理asp.net mvc 2.0或3.0中的错误
对于控制器范围错误,请尝试使用自定义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
}
}
Run Code Online (Sandbox Code Playgroud)
然后用属性装饰控制器,错误处理应该是你的
[RedirectOnError]
public class TestController : Controller
{
//.. Actions etc...
}
Run Code Online (Sandbox Code Playgroud)
如果错误与路由有关,则无效 - 即它首先找不到控制器.为此尝试Global.asax中的Application Error处理程序ie
protected void Application_Error(object sender, EventArgs e)
{
//.. perhaps direct to a custom error page is here
}
Run Code Online (Sandbox Code Playgroud)
我不知道这是否是"最佳做法".有用吗
归档时间: |
|
查看次数: |
23390 次 |
最近记录: |