解决方法
对于控制器范围错误,尝试使用自定义异常属性,即
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 } }
[RedirectOnError] public class TestController : Controller { //.. Actions etc... }
如果错误是通过路由,就不会有帮助 – 即它首先找不到控制器.为此,请尝试Global.asax中的应用程序错误处理程序,即
protected void Application_Error(object sender,EventArgs e) { //.. perhaps direct to a custom error page is here }
我不知道这是否是最佳实践.工作.