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>