asp.net mvc 中的异常停止 运行

Exception stops running in asp.net mvc

Global.asax.cs:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();    
    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);

    GlobalFilters.Filters.Add(new HandleErrorAttribute()); // i added this 
}

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();

    //return new EmptyResult();
}

操作:

public ActionResult Index()
{
    var a = Convert.ToInt64(""); // I get exception after that project is not running

    return view();
}

问题:

我正在尝试在 asp.net MVC 中使用动态异常。为了做到这一点,我在 global.asax.cs 中添加了一个方法。异常处理有效,但项目在异常发生后 运行 无效。

当我遇到异常时,我希望项目保持 运行ning,就像使用 try-catch 语句时一样,但是当我遇到异常时,项目停止工作。

要添加什么或要更改什么以便项目继续运行宁?

当引发异常时,它会通过调用堆栈传递,直到被捕获。 如果未被捕获,则应用程序停止 运行.

在这里您不尝试捕获异常,这就是您的应用程序停止的原因 运行。

您只需要:

public ActionResult Index()
{
    try
    {
        var a = Convert.ToInt64("");
    }
    catch (YourExceptionType ex)
    {
       // Do something
    }

    return view();
}

但是你也可以使用TryParse的方法,这样就避免了异常机制的使用,异常机制比较繁琐。

另请记住,捕获异常意味着您知道如何使您的应用程序保持稳定状态,即使出现该异常也是如此。

总结:

Application_Error 不处理您的代码抛出的异常类型,即使它处理了您也不会 return 任何东西。

详情:

您正在尝试在 ASP.NET MVC 中使用异常处理的两个不同方面。

如果您在 GlobalFilters 中注册 HandlerErrorAttribute 那么您是说对于任何未捕获的错误您想要重定向到应用程序的错误页面,默认情况下可以在 /Views/SharedFolder .

但这只适用于 web.config 中的 customErrors="On":

<system.web>
    <customErrors mode="On" />
</system.web>

请注意,您还可以在 Controller 或 ActionMethod 级别而非全局应用 HandlerErrorAttribute

如果 customErrors="On" 但您没有在 /Views/SharedFolder 中定义错误页面 那么它将抛出一个类型为 [=19 的复合错误=] 反过来会冒泡到 Application_Error.

如果另一方面,customErrors="Off" 那么 HandleErrorAttribute 机制将不会触发,而是由您的 Index ActionMethod 触发的异常将冒泡到您在中定义的 GlobalError 处理程序Application_Error。

在这种情况下,异常将与您的代码相关:

var a = Convert.ToInt64("");

这将抛出类型为 System.InvalidFormatException 的异常。

因此,如果您在 Application_Error 中设置断点,您将看到此方法执行 运行 但它实际上不会执行任何操作,因为您的 switch 语句仅假设 httpException:

 HttpException httpException = exception as HttpException;
 if (httpException != null)
在这些情况下,

httpException 将始终为 null,因为 System.InvalidOperationExceptionSystem.InvalidFormatException 都不是从 HttpException 继承的。

所以你需要做的事情更像是:

HttpException httpException = exception as HttpException;

if (httpException == null)
{
  // General exception handling logic here
}
else            
{
  // Http exception handling switch statement here            
}

这就是说,即使您正确地捕获并处理了错误,您也没有对它或之后做任何事情:

//return new EmptyResult();

所以你仍然会得到一个空白页面。此时您应该执行重定向或 Server.Transfer 或 某事