如何为ASP创建一个全局自定义错误页面。净MVC3吗?

时间:2022-01-09 06:14:15

I brand new to ASP.NET MVC3. How would I create a global custom error page for MVC3? The general idea is when an exception is thrown it would show a generic message to the user and it would log the exception to a database for developers to investigate later.

我对ASP很陌生。净MVC3。如何为MVC3创建一个全局自定义错误页面?一般的想法是,当抛出异常时,它将向用户显示通用消息,并将异常记录到数据库中,供开发人员稍后研究。

Thanks in advance for your help.

谢谢你的帮助。

Here is what I ended up doing in global.asax.cs:

以下是我在全球做的事情。

    protected void Application_Error()
    {
        var exception = Server.GetLastError();

        Log.Error("Exception", exception);

        var httpException = exception as HttpException;
        Response.Clear();
        Server.ClearError();
        var routeData = new RouteData();
        routeData.Values["controller"] = "Error";
        routeData.Values["action"] = "General";
        routeData.Values["exception"] = exception;
        Response.StatusCode = 500;
        if (httpException != null)
        {
            Response.StatusCode = httpException.GetHttpCode();
            switch (Response.StatusCode)
            {
                case 403:
                    routeData.Values["action"] = "Http403";
                    break;
                case 404:
                    routeData.Values["action"] = "Http404";
                    break;
            }
        }

        IController errorsController = new ErrorController();
        var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
        errorsController.Execute(rc);
    }

3 个解决方案

#1


1  

In your Global.asax file implement the Application_Error method:

在你的全球。asax文件实现Application_Error方法:

    protected void Application_Error() { 
        HttpContext ctx = HttpContext.Current; 
        var error = ctx.Server.GetLastError();
        ctx.Response.Clear(); 
        ctx.Response.End(); 
    }

Following up on Maess' comment:

跟进Maess的评论:

Read this: Error Handling in asp.net mvc 3

请阅读:asp.net mvc 3中的错误处理

#2


1  

What I've done in my project is I created a BaseController and overridden the OnException event as below,

在我的项目中,我创建了一个BaseController,并重写了OnException事件,如下所示,

protected override void OnException(ExceptionContext filterContext)
{
    // do some logging using log4net or signal to ELMAH
    filterContext.ExceptionHandled = true;
    var exModel = new HandleErrorInfo(filterContext.Exception,  
                  filterContext.RouteData.Values["controller"].ToString(), 
                  filterContext.RouteData.Values["action"].ToString());
    View("Error", exModel).ExecuteResult(ControllerContext);
}

Also I removed the HandleError action filter registered in the Global.asax.cs.

我还删除了在Global.asax.cs中注册的HandleError动作过滤器。

Note: You should have a view with name Error in shared folder.

注意:在共享文件夹中应该有一个名称错误的视图。

Update: To extract the error information from the Error view you have to bind the Error view to the model HandleErrorInfo.

更新:要从错误视图中提取错误信息,您必须将错误视图绑定到模型HandleErrorInfo。

@model System.Web.Mvc.HandleErrorInfo

Then you can easily access the exception anywhere in the view as

然后,您可以轻松访问视图中任意位置的异常

@Model.Exception

#3


0  

Create a view called Error and add it to your Views\Shared folder. Then in your catch blocks, after you have logged the error, redirect the action to the error view. If you want to display a sophisticated message create an Error model and pass it to the view, or put the information into the ViewBag.

创建一个名为Error的视图,并将其添加到视图\共享文件夹中。然后在catch块中,在记录错误之后,将操作重定向到error视图。如果您希望显示复杂的消息,请创建一个错误模型并将其传递给视图,或者将信息放入ViewBag。

For information on unhandled exceptions and HTTP errors see Ropstah's post.

有关未处理异常和HTTP错误的信息,请参阅Ropstah的文章。

#1


1  

In your Global.asax file implement the Application_Error method:

在你的全球。asax文件实现Application_Error方法:

    protected void Application_Error() { 
        HttpContext ctx = HttpContext.Current; 
        var error = ctx.Server.GetLastError();
        ctx.Response.Clear(); 
        ctx.Response.End(); 
    }

Following up on Maess' comment:

跟进Maess的评论:

Read this: Error Handling in asp.net mvc 3

请阅读:asp.net mvc 3中的错误处理

#2


1  

What I've done in my project is I created a BaseController and overridden the OnException event as below,

在我的项目中,我创建了一个BaseController,并重写了OnException事件,如下所示,

protected override void OnException(ExceptionContext filterContext)
{
    // do some logging using log4net or signal to ELMAH
    filterContext.ExceptionHandled = true;
    var exModel = new HandleErrorInfo(filterContext.Exception,  
                  filterContext.RouteData.Values["controller"].ToString(), 
                  filterContext.RouteData.Values["action"].ToString());
    View("Error", exModel).ExecuteResult(ControllerContext);
}

Also I removed the HandleError action filter registered in the Global.asax.cs.

我还删除了在Global.asax.cs中注册的HandleError动作过滤器。

Note: You should have a view with name Error in shared folder.

注意:在共享文件夹中应该有一个名称错误的视图。

Update: To extract the error information from the Error view you have to bind the Error view to the model HandleErrorInfo.

更新:要从错误视图中提取错误信息,您必须将错误视图绑定到模型HandleErrorInfo。

@model System.Web.Mvc.HandleErrorInfo

Then you can easily access the exception anywhere in the view as

然后,您可以轻松访问视图中任意位置的异常

@Model.Exception

#3


0  

Create a view called Error and add it to your Views\Shared folder. Then in your catch blocks, after you have logged the error, redirect the action to the error view. If you want to display a sophisticated message create an Error model and pass it to the view, or put the information into the ViewBag.

创建一个名为Error的视图,并将其添加到视图\共享文件夹中。然后在catch块中,在记录错误之后,将操作重定向到error视图。如果您希望显示复杂的消息,请创建一个错误模型并将其传递给视图,或者将信息放入ViewBag。

For information on unhandled exceptions and HTTP errors see Ropstah's post.

有关未处理异常和HTTP错误的信息,请参阅Ropstah的文章。