获得;对象引用未设置为实例";在HttpContext.Current.Application中

获得;对象引用未设置为实例";在HttpContext.Current.Application中,httpcontext,Httpcontext,我几乎不知道如何解决这个问题。。我想在服务器关闭时捕获错误,并将页面重定向到Maintenance.aspx。但它在HttpContext.Current.Application[“ErrorCode”].ToString()处出错。请帮我解决这个问题 非常感谢 try{ // method here } catch (Exception ex) { Panel1.Visible = true; string statuscode

我几乎不知道如何解决这个问题。。我想在服务器关闭时捕获错误,并将页面重定向到
Maintenance.aspx
。但它在
HttpContext.Current.Application[“ErrorCode”].ToString()处出错。请帮我解决这个问题

非常感谢

try{
// method here
}
 catch (Exception ex)
       {
            Panel1.Visible = true;
            string statuscode = HttpContext.Current.Application["ErrorCode"].ToString(); //Getting error here!
            if (statuscode != null || statuscode != string.Empty)
            {
                if (statuscode == "500")
                {
                    lblDetailMsg.Text = "<b>Error Page- <b> " + HttpContext.Current.Application["ErrorPage"].ToString() + " <br /> <b>Error Message-</b> The Requested Page was not found.";
                    Response.Redirect("Maintenance.aspx");
                }
            }

        }
试试看{
//这里的方法
}
捕获(例外情况除外)
{
面板1.可见=真实;
string statuscode=HttpContext.Current.Application[“ErrorCode”].ToString();//此处出现错误!
if(statuscode!=null | | statuscode!=string.Empty)
{
如果(状态代码=“500”)
{
lblDetailMsg.Text=“错误页面-”+HttpContext.Current.Application[“ErrorPage”].ToString()+“
错误消息-找不到请求的页面。”; 响应重定向(“Maintenance.aspx”); } } }
如果我正确理解了您的代码,那么错误一定是由于.toString()方法引起的

试试这个

string statuscode = Convert.ToString(HttpContext.Current.Application["ErrorCode"]); // added Convert.Tostring()
    if (statuscode != null || statuscode != string.Empty)
    {
        if (statuscode == "500")
        {
             lblDetailMsg.Text = "<b>Error Page- <b> " + Convert.ToString(HttpContext.Current.Application["ErrorPage"]) + " <br /> <b>Error Message-</b> The Requested Page was not found.";
             Response.Redirect("Maintenance.aspx");
        }
    }
string statuscode=Convert.ToString(HttpContext.Current.Application[“ErrorCode”]);//添加了Convert.Tostring()
if(statuscode!=null | | statuscode!=string.Empty)
{
如果(状态代码=“500”)
{
lblDetailMsg.Text=“错误页面-”+Convert.ToString(HttpContext.Current.Application[“ErrorPage”])+“
错误消息-找不到请求的页面。”; 响应重定向(“Maintenance.aspx”); } }
Convert.ToString()处理null,而ToString()不处理null

在这里,HttpContext.Current.Application[“ErrorCode”]似乎不包含“Error Code”的值,因此它给出了null值