处理Global.asax Application_Error中的错误时出现NotImplementedException

时间:2012-08-06 14:39:04

标签: asp.net-mvc-3 c#-4.0 exception-handling

我有代码

protected void Application_Error(object sender, EventArgs e)
    {
        HttpContext ctx = HttpContext.Current;
        Exception ex = ctx.Server.GetLastError();
        ctx.Response.Clear();

        var viewResult = new ViewResult();

        if (ex.GetType() != typeof(SecurityException))
        {
            RequestContext rc = ((MvcHandler)ctx.CurrentHandler).RequestContext;
            IController controller = new ModuleController();
            var context = new ControllerContext(rc, (ControllerBase)controller);                

            var httpException = ex as HttpException;
            if (httpException != null)
            {
                switch (httpException.GetHttpCode())
                {
                    case 404:
                        viewResult.ViewName = "Error404";
                        Response.StatusCode = 404;
                        break;

                    case 500:
                        viewResult.ViewName = "Error500";
                        Response.StatusCode = 500;
                        break;

                    default:
                        viewResult.ViewName = "Error";
                        Response.StatusCode = httpException.GetHttpCode();
                        break;
                }
            }
            else
            {
                viewResult.ViewName = "Error";
            }

            viewResult.ViewData.Model = new HandleErrorInfo(ex, context.RouteData.GetRequiredString("controller"), context.RouteData.GetRequiredString("action"));
            viewResult.ExecuteResult(context);
            ctx.Server.ClearError();
        }
        else
        {
            var context = new ControllerContext { RouteData = new RouteData() };
            context.RouteData.Values.Add("controller", "Security");
            context.Controller = new SecurityController();
            HttpCookie cookie = this.mebershipService.GetCookie();
            cookie.Expires = DateTime.Now.AddDays(-1);
            this.Context.Response.Cookies.Add(cookie);
            viewResult.ViewName = "HackedDetect";
            Response.StatusCode = 403;
            viewResult.ExecuteResult(context);
            ctx.Server.ClearError();
        }
    }

处理SecurityException时,与代码viewResult.ExecuteResult(context)上的系统无关的调用控制器(为了不绕圈)是一个NotImplementedException 但我不清楚哪种方法或操作没有定义。

这是从错误中复制到剪贴板的内容

  

System.NotImplementedException未由用户代码处理
  HResult = -2147467263
  消息=未实施方法或操作   来源= System.Web
  堆栈跟踪:
  在System.Web.HttpContextBase.get_Response()中   在System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)中   在SGN.Web.MvcApplication.Application_Error(Object sender,EventArgs e)中的C:\ Project \ SGN \ SGN.Web \ Global.asax.cs:第142行
  在System.EventHandler.Invoke(Object sender,EventArgs e)中   在System.Web.HttpApplication.RaiseOnError()中   InnerException:

更新

我会写我做的是为了得到这个错误

我创建了自定义Identity类,并在初始化时,我验证了cookie。如果cookie未验证,我创建SecurityException

0 个答案:

没有答案