如果你的网站出现一堆让人看不懂的报错,那么你就不是一个合格的程序员,也不是一个合格的站长。
下面的方面可以帮助你的网站远离让人头大的页面。
第一步:配置web.config
打开web.config,在
第二步:建立错误页面
在网站根目录下建立404页面(找不到页面):404.html
403页面(服务器禁止访问):403.html
这样就可以解决一部分问题,但是如果我们程序有些bug,碰巧被用户发现,那么还是会返回给用户一个不友好的报错页面。所以我们还要建立一个ErrorPages.aspx捕捉那些我们不知道的错误页面,用以处理那些报错,显示给用户良好的页面。
第三步:捕捉未知错误,显示友好提示信息。
在ErrorPages.aspx.cs中加入以下代码:
[c-sharp] view plain copy
- if (!IsPostBack)
- {
- HttpException erroy = new HttpException();
- string strCode = erroy.ErrorCode.ToString();
- string strMsg = erroy.Message;
- StringBuilder sb = new StringBuilder();
- sb.Append("-----------记录开始时间:" + System.DateTime.Now+"-----------------
"); - erroy.HelpLink = Request.QueryString["aspxerrorpath"];
- sb.Append("ErrorCode:" + strCode + "
"); - sb.Append("Message:" + strMsg + "
"); - sb.Append("HelpLink:" + erroy.HelpLink + "
"); - sb.Append("Source:" + erroy.Source + "
"); - sb.Append("TargetSite:" + erroy.TargetSite + "
"); - sb.Append("InnerException:" + erroy.InnerException + "
"); - sb.Append("StackTrace:" + erroy.StackTrace + "
"); - sb.Append("GetHtmlErrorMessage:" + erroy.GetHtmlErrorMessage() + "
"); - sb.Append("erroy.GetHttpCode().ToString():" + erroy.GetHttpCode().ToString() + "
"); - sb.Append("erroy.Data.ToString():" + erroy.Data.ToString() + "
"); - sb.Append("----------记录结束----------------");
- Response.Write(sb.ToString());
- }
到此为止:网站错误配置完成。当然错误处理页面你可以随意定义,你可以把捕捉到的错误写入数据库或者文件,只显示一些提示信息给用户,你也可以把错误信息处理后友好的显示给用户。
还有一种方法是在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
- protected void Application_Error(Object sender, EventArgs e)
- {
- System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~/Web.config");
- System.Web.Configuration.CustomErrorsSection customErrors = (System.Web.Configuration.CustomErrorsSection)config.GetSection("system.web/customErrors");
- if (customErrors != null && (customErrors.Mode == System.Web.Configuration.CustomErrorsMode.On || customErrors.Mode == System.Web.Configuration.CustomErrorsMode.RemoteOnly))
- {
- System.Web.HttpApplication app = (HttpApplication)sender;
- System.Exception lastError = app.Server.GetLastError();
- System.Web.HttpException httpEx = (HttpException)lastError;
- if (httpEx != null)
- {
- int httpErrorCode = httpEx.GetHttpCode();
- string redirect = customErrors.DefaultRedirect;
- foreach (System.Web.Configuration.CustomError error in customErrors.Errors)
- {
- if (error.StatusCode == httpErrorCode) redirect = error.Redirect;
- }
- app.Server.ClearError();
- app.Context.Response.StatusCode = httpErrorCode;
- Server.Transfer(redirect);
- }
- }
- }
这样问题就得以解决了。

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

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

htmlattributesarecrucialinwebdevelopment forcontrollingBehavior,外觀和功能

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

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

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

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

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


熱AI工具

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

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

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

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

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

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

禪工作室 13.0.1
強大的PHP整合開發環境

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

Dreamweaver Mac版
視覺化網頁開發工具