Asp.net HttpContext.AllErrors何时包含多个异常?

Asp.net HttpContext.AllErrors何时包含多个异常?,asp.net,error-handling,httpcontext,Asp.net,Error Handling,Httpcontext,在ASP.NET应用程序中,典型的错误处理代码包括,但是也有一个集合,其GetLastError()方法仅检索第一个集合。在哪些情况下,AllErrors集合可能包含多个异常?我想不出什么,但显然它是有目的的…ASP.NET框架支持一个不同的模型,其中一个请求可能会遇到多个错误,所有这些错误都可以在不停止请求处理的情况下报告,从而允许向用户提供更细微和有用的信息 namespace ErrorHandling { // sample class adding errors to Http

在ASP.NET应用程序中,典型的错误处理代码包括,但是也有一个集合,其
GetLastError()
方法仅检索第一个集合。在哪些情况下,
AllErrors
集合可能包含多个异常?我想不出什么,但显然它是有目的的…

ASP.NET框架支持一个不同的模型,其中一个请求可能会遇到多个错误,所有这些错误都可以在不停止请求处理的情况下报告,从而允许向用户提供更细微和有用的信息

namespace ErrorHandling
{
    // sample class adding errors to HttpContext
    public partial class SumControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                int? first = GetIntValue("first");
                int? second = GetIntValue("second");
                if (first.HasValue && second.HasValue)
                {
                    //result.InnerText = (first.Value + second.Value).ToString();
                    //resultPlaceholder.Visible = true;
                }
                else
                {
                    Context.AddError(new Exception("Cannot perform calculation"));
                }
            }
        }

        private int? GetIntValue(string name)
        {
            int value;
            if (Request[name] == null)
            {
                Context.AddError(new ArgumentNullException(name));
                return null;
            }
            else if (!int.TryParse(Request[name], out value))
            {
                Context.AddError(new ArgumentOutOfRangeException(name));
                return null;
            }
            return value;
        }
    }
}
// intercepting the errors
public class Global : System.Web.HttpApplication
{
    protected void Application_EndRequest(object sender, EventArgs e)
    {
        if (Context.AllErrors != null && Context.AllErrors.Length > 1)
        {
            Response.ClearHeaders();
            Response.ClearContent();
            Response.StatusCode = 200;
            Server.Execute("/MultipleErrors.aspx");
            Context.ClearError();
        }
    }
}
// MultipleErrors code behind
public partial class MultipleErrors : System.Web.UI.Page
{
    public IEnumerable<string> GetErrorMessages()
    {
        return Context.AllErrors.Select(e => e.Message);
    }
}
命名空间错误处理
{
//向HttpContext添加错误的示例类
公共部分类SumControl:System.Web.UI.UserControl
{
受保护的无效页面加载(对象发送方、事件参数e)
{
如果(iPostBack)
{
int?first=GetIntValue(“first”);
int?second=GetIntValue(“second”);
if(first.HasValue&&second.HasValue)
{
//result.InnerText=(first.Value+second.Value).ToString();
//resultPlaceholder.Visible=true;
}
其他的
{
AddError(新异常(“无法执行计算”);
}
}
}
私有int?GetIntValue(字符串名称)
{
int值;
if(请求[名称]==null)
{
AddError(新参数nullexception(name));
返回null;
}
else如果(!int.TryParse(请求[名称],输出值))
{
AddError(新ArgumentOutOfRangeException(名称));
返回null;
}
返回值;
}
}
}
//拦截错误
公共类全局:System.Web.HttpApplication
{
受保护的无效应用程序\u EndRequest(对象发送方,事件参数e)
{
if(Context.AllErrors!=null&&Context.AllErrors.Length>1)
{
Response.ClearHeaders();
Response.ClearContent();
Response.StatusCode=200;
Execute(“/MultipleErrors.aspx”);
Context.ClearError();
}
}
}
//多重错误代码隐藏
公共部分类多个错误:System.Web.UI.Page
{
公共IEnumerable GetErrorMessages()
{
返回Context.AllErrors.Select(e=>e.Message);
}
}

答案是大量引用appress中的pro asp.net 4.5,但这只会出现在我自己的代码中(如果我不这么做,我不需要担心)。ASP.NET本身并没有这样做?这进一步解释了我的想法:请注意,在生成错误页面之后,我们调用了HttpContext.ClearError方法。调用AddError方法不会触发错误事件,但会触发我们在本章前面的Web.config文件中配置的内置ASP.NET错误处理支持。如果我们不调用ClearError,那么我们的多错误页面将替换为Web.config文件指定的任何内容。这不是更倾向于进入
ModelState
?因此您可以将上述内容视为手动编写的异常驱动错误页面,在没有asp.net/IIS干预的情况下,我们通过调用Context.ClearError()在自己之后进行清除;嗯。。。检查我的下一个问题: