搜尋
首頁web前端html教學.net三步配置错误页面,让你的网站远离不和谐的页面_html/css_WEB-ITnose

如果你的网站出现一堆让人看不懂的报错,那么你就不是一个合格的程序员,也不是一个合格的站长。

下面的方面可以帮助你的网站远离让人头大的页面。

第一步:配置web.config

打开web.config,在节点下添加如下代码:

 
           
           
  

 

第二步:建立错误页面

在网站根目录下建立404页面(找不到页面):404.html

403页面(服务器禁止访问):403.html

这样就可以解决一部分问题,但是如果我们程序有些bug,碰巧被用户发现,那么还是会返回给用户一个不友好的报错页面。所以我们还要建立一个ErrorPages.aspx捕捉那些我们不知道的错误页面,用以处理那些报错,显示给用户良好的页面。

 

第三步:捕捉未知错误,显示友好提示信息。

在ErrorPages.aspx.cs中加入以下代码:

 

[c-sharp]  view plain copy

  1. if (!IsPostBack)  
  2.        {  
  3.            HttpException erroy = new HttpException();  
  4.            string strCode = erroy.ErrorCode.ToString();  
  5.            string strMsg = erroy.Message;  
  6.            StringBuilder sb = new StringBuilder();  
  7.              
  8.            sb.Append("-----------记录开始时间:" + System.DateTime.Now+"-----------------
    ");  
  9.            erroy.HelpLink = Request.QueryString["aspxerrorpath"];  
  10.            sb.Append("ErrorCode:" + strCode + "
    ");  
  11.            sb.Append("Message:" + strMsg + "
    ");  
  12.            sb.Append("HelpLink:" + erroy.HelpLink + "
    ");  
  13.            sb.Append("Source:" + erroy.Source + "
    ");  
  14.            sb.Append("TargetSite:" + erroy.TargetSite + "
    ");  
  15.            sb.Append("InnerException:" + erroy.InnerException + "
    ");  
  16.            sb.Append("StackTrace:" + erroy.StackTrace + "
    ");  
  17.            sb.Append("GetHtmlErrorMessage:" + erroy.GetHtmlErrorMessage() + "
    ");  
  18.            sb.Append("erroy.GetHttpCode().ToString():" + erroy.GetHttpCode().ToString() + "
    ");  
  19.            sb.Append("erroy.Data.ToString():" + erroy.Data.ToString() + "
    ");  
  20.            sb.Append("----------记录结束----------------");  
  21.   
  22.            Response.Write(sb.ToString());  
  23.        }  

到此为止:网站错误配置完成。当然错误处理页面你可以随意定义,你可以把捕捉到的错误写入数据库或者文件,只显示一些提示信息给用户,你也可以把错误信息处理后友好的显示给用户。

 

还有一种方法是在Global.asax中的void Application_Error(object sender, EventArgs e)方法中定义;现给以大体方法,具体操作可以根据实际情况给以修改。

在Global.asax文件中修改:

  void Application_Error(object sender, EventArgs e)
    {
        //在出现未处理的错误时运行的代码
        Exception erroy = Server.GetLastError();
        string err = "出错页面是:" + Request.Url.ToString() + "";
        err += "异常信息:" + erroy.Message + "";
        err += "Source:" + erroy.Source + "";
        err += "StackTrace:" + erroy.StackTrace + "";
        //清除前一个异常 
        //Server.ClearError();
        //此处理用Session["ProError"]出错。所以用 Application["ProError"] 
        Application["erroy"] = err;
        //此处不是page中,不能用Response.Redirect("../ErrorPages.aspx");
        System.Web.HttpContext.Current.Response.Redirect(HttpContext.Current.Request.ApplicationPath + "/ErrorPages.aspx");

    }

在ErrorPages.aspx.cs文件中修改

    protected void Page_Load(object sender, EventArgs e)
    {
        //显示程序中的错误码

        if (!IsPostBack)
        {

            //显示程序中的错误码

            if (Application["erroy"] != null)
            {

                Response.Write(Application["erroy"].ToString());

            }

       }

   }

 补充:使用上述方法实现的跳转,返回的HTTP状态码全部是302,本来应该返回404的也给返回302.这样对搜索引擎优化很不利。所以我们应该在Global.asax文件中添加如下代码:

 

[c-sharp]  view plain copy

  1. protected void Application_Error(Object sender, EventArgs e)  
  2.        {  
  3.            System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config");  
  4.            System.Web.Configuration.CustomErrorsSection customErrors = (System.Web.Configuration.CustomErrorsSection)config.GetSection("system.web/customErrors");  
  5.            if (customErrors != null && (customErrors.Mode == System.Web.Configuration.CustomErrorsMode.On || customErrors.Mode == System.Web.Configuration.CustomErrorsMode.RemoteOnly))  
  6.            {  
  7.                System.Web.HttpApplication app = (HttpApplication)sender;  
  8.                System.Exception lastError = app.Server.GetLastError();  
  9.                System.Web.HttpException httpEx = (HttpException)lastError;  
  10.                if (httpEx != null)  
  11.                {  
  12.                    int httpErrorCode = httpEx.GetHttpCode();  
  13.                    string redirect = customErrors.DefaultRedirect;  
  14.                    foreach (System.Web.Configuration.CustomError error in customErrors.Errors)  
  15.                    {  
  16.                        if (error.StatusCode == httpErrorCode) redirect = error.Redirect;  
  17.                    }  
  18.                    app.Server.ClearError();  
  19.                    app.Context.Response.StatusCode = httpErrorCode;  
  20.                    Server.Transfer(redirect);  
  21.                }  
  22.            }  
  23.        }  

这样问题就得以解决了。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
HTML標籤和HTML屬性有什麼區別?HTML標籤和HTML屬性有什麼區別?May 14, 2025 am 12:01 AM

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

HTML的未來:進化和趨勢HTML的未來:進化和趨勢May 13, 2025 am 12:01 AM

HTML的未來將朝著更加語義化、功能化和模塊化的方向發展。 1)語義化將使標籤更明確地描述內容,提升SEO和無障礙訪問。 2)功能化將引入新元素和屬性,滿足用戶需求。 3)模塊化將支持組件化開發,提高代碼復用性。

為什麼HTML屬性對Web開發很重要?為什麼HTML屬性對Web開發很重要?May 12, 2025 am 12:01 AM

htmlattributesarecrucialinwebdevelopment forcontrollingBehavior,外觀和功能

Alt屬性的目的是什麼?為什麼重要?Alt屬性的目的是什麼?為什麼重要?May 11, 2025 am 12:01 AM

alt屬性是HTML中標籤的重要部分,用於提供圖片的替代文本。 1.當圖片無法加載時,alt屬性中的文本會顯示,提升用戶體驗。 2.屏幕閱讀器使用alt屬性幫助視障用戶理解圖片內容。 3.搜索引擎索引alt屬性中的文本,提高網頁的SEO排名。

HTML,CSS和JavaScript:示例和實際應用HTML,CSS和JavaScript:示例和實際應用May 09, 2025 am 12:01 AM

HTML、CSS和JavaScript在網頁開發中的作用分別是:1.HTML用於構建網頁結構;2.CSS用於美化網頁外觀;3.JavaScript用於實現動態交互。通過標籤、樣式和腳本,這三者共同構築了現代網頁的核心功能。

如何在標籤上設置lang屬性?為什麼這很重要?如何在標籤上設置lang屬性?為什麼這很重要?May 08, 2025 am 12:03 AM

設置標籤的lang屬性是優化網頁可訪問性和SEO的關鍵步驟。 1)在標籤中設置lang屬性,如。 2)在多語言內容中,為不同語言部分設置lang屬性,如。 3)使用符合ISO639-1標準的語言代碼,如"en"、"fr"、"zh"等。正確設置lang屬性可以提高網頁的可訪問性和搜索引擎排名。

HTML屬性的目的是什麼?HTML屬性的目的是什麼?May 07, 2025 am 12:01 AM

htmlattributeseresene forenhancingwebelements'functionalityandAppearance.TheyAdDinformationTodeFineBehavior,外觀和互動,使網站互動,響應式,visalalyAppealing.AttributesLikutesLikeSlikEslikesrc,href,href,href,類,類型,類型,和dissabledtransfransformformformformformformformformformformformformformformforment

您如何在HTML中創建列表?您如何在HTML中創建列表?May 06, 2025 am 12:01 AM

toCreateAlistInHtml,useforforunordedlistsandfororderedlists:1)forunorderedlists,wrapitemsinanduseforeachItem,RenderingeringAsabulletedList.2)fororderedlists,useandfornumberedlists,useandfornumberedlists,casundfornumberedlists,casundfornthetthetthetthetthetthetthetttributefordforderfordforderforderentnumberingsnumberingsnumberingStys。

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具