Error处理程序的Global

Error处理程序的Global

本文介绍了ASP.NET MVC的Application_Error处理程序的Global.asax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Global.asax中我们有一个类类型System.Web.HttpApplication名为MvcApplication
那些重新presents应用程序并在其中我们可以处理各种事件。

我感兴趣的Application_Error处理程序。
在此处理程序,我们可以使用类MvcApplication的所有属性。

-1 -

时总是真的'(MvcApplication)发件人'和'本'是同一个对象?

 保护无效的Application_Error(对象发件人,EventArgs的发送)
{
  VAR httpApp =(MvcApplication)发送;
  VAR equality1 = httpApp ==这一点; //总是如此?
}

-2 -

什么是得到错误的最好方法?
下面的示例返回相同的错误?

 异常EX0 = this.Context.Error;
异常EX1 = httpContext.Error;
例外EX2 = Server.GetLastError();
VAR equality3 = EX1 == EX2; //真的吗?


解决方案

 保护无效的Application_Error(对象发件人,EventArgs的发送){
  异常异常= Server.GetLastError();
  Response.Clear();  HttpException httpException =异常作为HttpException;  如果(httpException!= NULL){
    串动;    开关(httpException.GetHttp code()){
      案例404:
        // 找不到网页
        行动=HttpError404;
        打破;
      案例500:
        // 服务器错误
        行动=HttpError500;
        打破;
      默认:
        行动=一般;
        打破;
      }      //服务器上清除错误
      Server.ClearError();      的Response.Redirect(的String.Format(〜/错误/ {0} /消息= {1}?,行动,exception.Message));
    }

和在你的控制器:

  // GET:/错误/ HttpError404
公众的ActionResult HttpError404(字符串消息){
   返回视图(SomeView,邮件);
}

ASP.NET MVC自定义错误处理的Application_Error Global.asax中?

In Global.asax we have a class of type System.Web.HttpApplication named MvcApplicationthat represents the application and in which we can handle various events.

I'm interested in the Application_Error handler.In this handler we can use all the properties of the class MvcApplication.

-1-

Is always true that '(MvcApplication)sender' and 'this' are the same object?

protected void Application_Error(object sender, EventArgs e)
{
  var httpApp = (MvcApplication)sender;
  var equality1 = httpApp == this; // always true?
}

-2-

What is the best way to get the error?The following examples return the same error?

Exception ex0 = this.Context.Error;
Exception ex1 = httpContext.Error;
Exception ex2 = Server.GetLastError();
var equality3 = ex1 == ex2; // true?
解决方案
protected void Application_Error(object sender, EventArgs e) {
  Exception exception = Server.GetLastError();
  Response.Clear();

  HttpException httpException = exception as HttpException;

  if (httpException != null) {
    string action;

    switch (httpException.GetHttpCode()) {
      case 404:
        // page not found
        action = "HttpError404";
        break;
      case 500:
        // server error
        action = "HttpError500";
        break;
      default:
        action = "General";
        break;
      }

      // clear error on server
      Server.ClearError();

      Response.Redirect(String.Format("~/Error/{0}/?message={1}", action, exception.Message));
    }

And in your controller:

// GET: /Error/HttpError404
public ActionResult HttpError404(string message) {
   return View("SomeView", message);
}

ASP.NET MVC Custom Error Handling Application_Error Global.asax?

这篇关于ASP.NET MVC的Application_Error处理程序的Global.asax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:24