如果你的网站出现一堆让人看不懂的报错,那么你就不是一个合格的程序员,也不是一个合格的站长。
下面的方面可以帮助你的网站远离让人头大的页面。
第一步:配置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,customeizableWithTheTtheTthetTheTeTeptTributeFordTributeForderForderForderFerentNumberingSnumberingStyls。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

禅工作室 13.0.1
功能强大的PHP集成开发环境

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器