网站项目异常_系统级捕获_404页面跳转_初步认识

本文介绍了在C#中如何直接抛出异常,并通过代码捕捉和处理异常,同时展示了如何在.NET Web项目中使用Global项目启动页进行异常处理,以及如何在web.config中配置异常404跳转。

前言:本人对异常的捕获是个初学者,今天刚有所接触,做个简要记录,以后会逐步完善,不足之处请批评指正:

 

项目中使用C#代码直接抛出一个异常:

 throw new ArgumentException("错误的压缩级别");

 

A-代码捕捉异常处理:.net Web项目,Global项目启动页,项目异常处理代码:

protected void Application_Error(object sender, EventArgs e)
        {
            if (this.Request.FilePath == "/")
            {
                this.Server.ClearError();
                return;
            }
            Exception error = this.Server.GetLastError();
            Exception exception = error.InnerException ?? error;
            if (exception is HttpException)
            {
                if ((exception as HttpException).GetHttpCode() == HttpStatusCode.NotFound.GetValue())
                {
                    return;
                }
            }
            _Log.Error("\r\nClient IP:" + this.Request.UserHostAddress + "\r\nError Page:" + this.Request.Url, exception);
            HttpContext context = this.Context;
            string userName = context.User == null ? "未登录用户" : context.User.Identity.Name;
            string referrer = Request.UrlReferrer == null ? string.Empty : this.Request.UrlReferrer.AbsoluteUri;
            //this.Context.Items.Add("exception", exception);// <customErrors redirectMode="ResponseRewrite" defaultRedirect="~/404.html" mode="On">
        }

 

 

B:配置文件项目异常404跳转处理:.net Web项目,web.config配置异常捕获:

 <httpErrors>
      <remove statusCode="404" subStatusCode="-1" />
      <error statusCode="404" prefixLanguageFilePath="" path="/404.html" responseMode="ExecuteURL" />
      <error statusCode="404" path="/404.html" responseMode="ExecuteURL" subStatusCode="1" />
      <error statusCode="404" path="/fr/404.html" responseMode="ExecuteURL" subStatusCode="2" />
      <error statusCode="404" path="/es/404.html" responseMode="ExecuteURL" subStatusCode="3" />
      <error statusCode="404" path="/ja/404.html" responseMode="ExecuteURL" subStatusCode="4" />
      <error statusCode="404" path="/ar/404.html" responseMode="ExecuteURL" subStatusCode="5" />
      <error statusCode="404" path="/de/404.html" responseMode="ExecuteURL" subStatusCode="6" />
      <error statusCode="404" path="/it/404.html" responseMode="ExecuteURL" subStatusCode="7" />
    </httpErrors>

 

转载于:https://www.cnblogs.com/lxhbky/p/10479363.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值