asp.net mvc 全局异常过滤器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using PS.Core.Commons;
using PS.Web.Mvc.Extensions;

namespace PS.Web.Mvc
{
    /// 
    /// 全局异常,记录异常信息“Log4net.Error”
    /// 
    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class GlobalErrorAttribute : HandleErrorAttribute
    {
        public override void OnException(ExceptionContext filterContext)
        {
            Log4net.Error(filterContext.Controller, filterContext.Exception.Message, filterContext.Exception);

            string x_requested_with = filterContext.HttpContext.Request.Headers["X-Requested-With"];
            string request_type     = filterContext.HttpContext.Request.Headers["ps-request-type"];
            if ((!string.IsNullOrEmpty(x_requested_with) && x_requested_with.ToLower() == "xmlhttprequest") ||
                (!string.IsNullOrEmpty(request_type)     && request_type.ToLower()     == "zdy-ajax"))
            {
                filterContext.Result = new PsErrorResult(filterContext.Exception, 1);//继承至ContentResult
            }
            else
            {
                PsController controller = (PsController)filterContext.Controller;
                filterContext.Result = new RedirectResult(controller.Url.PsAction("","Error","error"));
            }

            filterContext.ExceptionHandled = true;
        }
    }
}
/// 
/// 框架基础控制器
/// 
[GlobalError]
public class PsController : Controller
{}

你可能感兴趣的:(C#,ASP.NET,MVC,c#,asp.net,后端)