MVC控制器(Controller)异常处理继承了IExceptionFilter的OnException内容

MVC控制器(Controller)异常处理继承了IExceptionFilter的OnException内容


代码如下:

<span style="font-size:12px;">using System;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace ChapterFive.Controllers
{
    public class BaseController : Controller
    {
        //
        // GET: /Base/

        public ActionResult Error()
        {   
            return View();
        }

        /// <summary>
        /// 捕捉未经处理的异常
        /// 暂时本地测试使用,
        /// add by wh at 2014-10-27
        /// </summary>
        /// <param name="filterContext"></param>
        protected override void OnException(ExceptionContext filterContext)
        {
            try
            {
                var sb = new StringBuilder();
                if (Request.UrlReferrer != null && !string.IsNullOrEmpty(Request.UrlReferrer.AbsoluteUri))
                    sb.Append("(上一页)Referrer:").AppendLine(Request.UrlReferrer.AbsoluteUri);


                if (!string.IsNullOrEmpty(Request.RawUrl))
                    sb.Append("(请求)RawUrl:").AppendLine(Request.RawUrl);

                string q = string.Empty;
                foreach (string key in Request.QueryString.AllKeys)
                {
                    if (!string.IsNullOrEmpty(Request.QueryString[key]))
                        q += key + "=" + HttpUtility.UrlDecode(Request.QueryString[key]) + "&";
                }
                if (!string.IsNullOrEmpty(q))
                    sb.Append("QueryString:").AppendLine(q);


                q = string.Empty;
                foreach (string key in Request.Form.AllKeys)
                {
                    if (!string.IsNullOrEmpty(Request.Form[key]))
                        q += key + "=" + Request.Form[key] + "&";
                }
                if (!string.IsNullOrEmpty(q))
                    sb.Append("Form:").AppendLine(q);


                q = string.Empty;
                foreach (string key in Request.Cookies.AllKeys)
                {
                    var httpCookie = Request.Cookies[key];
                    if (httpCookie != null && (Request.Cookies != null && !string.IsNullOrEmpty(httpCookie.Value)))
                        q += key + "=" + httpCookie.Value + "&";
                }
                if (!string.IsNullOrEmpty(q))
                    sb.Append("Cookie:").AppendLine(q);


                Exception e = filterContext.Exception;   
                sb.Append(e);

                //Todo:记日志 
                //loginstance.AddLog("Controller", "Error", sb.ToString());

            }
            catch
            {


            }

            base.OnException(filterContext);
        }

    }
}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值