C# 异常停止在asp.net mvc中运行

C# 异常停止在asp.net mvc中运行,c#,asp.net-mvc,exception,C#,Asp.net Mvc,Exception,Global.asax.cs: protected void Application_Start() { AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRou

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();
}
行动:

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();
}
问题:

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
中添加了一个方法。异常处理工作正常,但异常发生后项目不会运行

当我得到异常时,我希望项目继续运行,就像使用try-catch语句时一样,但是当我得到异常时,项目停止工作


为了使项目继续运行,要添加什么或更改什么??

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

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

您所需要的只是:

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

    return view();
}
但是您也可以使用
TryParse
方法,这将避免使用异常机制,这是一件非常繁重的事情


还要记住,捕获异常意味着您知道如何保持应用程序处于稳定状态,即使引发了该异常。

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

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

您所需要的只是:

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

    return view();
}
但是您也可以使用
TryParse
方法,这将避免使用异常机制,这是一件非常繁重的事情


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

摘要:

应用程序_错误不会处理代码抛出的类型的异常,即使处理了,也不会返回任何内容

详细信息:

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中使用异常处理的两个不同方面

如果您在GlobalFilter中注册了
HandlerErrorAttribute
,则表示对于任何未捕获的错误,您希望重定向到应用程序的错误页面,默认情况下,该页面将位于
/Views/SharedFolder

但这仅适用于web.config中的customErrors=“On”:

<system.web>
    <customErrors mode="On" />
</system.web>
这将引发类型为
System.InvalidFormatException
的异常

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

 HttpException httpException = exception as HttpException;
 if (httpException != null)
在这些情况下,httpException将始终为null,因为
System.InvalidOperationException
System.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();

所以你仍然会得到一个空白页。此时,您应该执行重定向、服务器传输或其他操作。

摘要:

应用程序_错误不会处理代码抛出的类型的异常,即使处理了,也不会返回任何内容

详细信息:

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中使用异常处理的两个不同方面

如果您在GlobalFilter中注册了
HandlerErrorAttribute
,则表示对于任何未捕获的错误,您希望重定向到应用程序的错误页面,默认情况下,该页面将位于
/Views/SharedFolder

但这仅适用于web.config中的customErrors=“On”:

<system.web>
    <customErrors mode="On" />
</system.web>
这将引发类型为
System.InvalidFormatException
的异常

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

 HttpException httpException = exception as HttpException;
 if (httpException != null)
在这些情况下,httpException将始终为null,因为
System.InvalidOperationException
System.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();

所以你仍然会得到一个空白页。此时,您应该执行重定向、服务器传输或其他操作。

如何使用try-catch-dynamic(所有项目中只有1个try-catch)?这就是asp.net机制允许您使用错误处理方法执行的操作。但这意味着应用程序将在引发错误的地方停止。这只允许您根据所处理的错误向用户显示不同的结果,而不是在下一条指令上继续执行。您的意思是无法在.net中对所有项目使用一次try-catch,或者您建议使用什么?我的意思是您不能使用全局try-catch,让执行在异常之后的下一条指令上继续,因为执行只能在异常发生的调用堆栈级别上继续。感谢您的帮助,但不是更好的解决方案?如何使用try-catch dynamic(所有项目中只有1个try-catch)?这就是asp.net机制允许您处理错误的方法。但这意味着应用程序将在引发错误的地方停止。这只允许您根据所处理的错误向用户显示不同的结果,而不是在下一条指令上继续执行。您的意思是无法在.net中对所有项目使用一次try-catch,或者您建议使用什么?我的意思是您不能使用全局try-catch,并且让执行在异常之后的下一条指令上继续,因为执行只能在调用堆栈级别继续,其中