1.全局异常判别
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)] public class EwHandleErrorAttribute : HandleErrorAttribute { public override void OnException(ExceptionContext filterContext) { if (ICTConfiguration.Debug) { base.OnException(filterContext); return; } if (filterContext.ExceptionHandled) { return; } if (filterContext.HttpContext.Response.IsRequestBeingRedirected) { return; } var httpCode = new HttpException(null, filterContext.Exception).GetHttpCode(); if (!ExceptionType.IsInstanceOfType(filterContext.Exception)) { return; } if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500) { return; } ExceptionHelper.LogException(filterContext.Exception, HttpContext.Current); bool isAjaxCall = string.Equals("XMLHttpRequest", filterContext.HttpContext.Request.Headers["x-requested-with"], StringComparison.OrdinalIgnoreCase); if (isAjaxCall) { string message = filterContext.Exception.Message; if (filterContext.Exception is HttpRequestValidationException) { message = "包含非法字符"; } filterContext.Result = new JsonResult() { JsonRequestBehavior = JsonRequestBehavior.AllowGet, Data = new { succeed = false, ret = httpCode, msg = message } }; } else { var controllerName = (string)filterContext.RouteData.Values["controller"]; var actionName = (string)filterContext.RouteData.Values["action"]; var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName); filterContext.Result = new ViewResult() { ViewName = View, MasterName = Master, ViewData = new ViewDataDictionary(model), TempData = filterContext.Controller.TempData }; filterContext.HttpContext.Response.Redirect("/500.html"); } filterContext.ExceptionHandled = true; filterContext.HttpContext.Response.Clear(); filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; filterContext.HttpContext.Server.ClearError(); } }
2.判别当前注册用户是否加入企业(包括是否登录):过滤器
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)] public class JoinEnterpriseAttribute : TokenAuthorizeAttribute { public WorkContext WorkContext { get { var workContext = (WorkContext)System.Web.HttpContext.Current.Items["__current__workcontext"]; if (workContext == null) { workContext = new WorkContext(); System.Web.HttpContext.Current.Items["__current__workcontext"] = workContext; } return workContext; } } public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext) { base.OnAuthorization(filterContext); if (!filterContext.HttpContext.Response.IsRequestBeingRedirected) { var entAuths = (EntAuthAttribute[])filterContext.ActionDescriptor.GetCustomAttributes(typeof(EntAuthAttribute), false); var centAuths = filterContext.ActionDescriptor.ControllerDescriptor.GetCustomAttributes(typeof(EntAuthAttribute), true); if (entAuths.Length == 0 && centAuths.Length == 0) { if (WorkContext != null && WorkContext.UserInfo != null && (WorkContext.CompanyId == 0 || string.IsNullOrWhiteSpace(WorkContext.UserInfo.Name))) { filterContext.HttpContext.Response.Redirect("/auth", true); } } } } }
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = false)] public class TokenAuthorizeAttribute : AuthorizeAttribute { public WorkContext WorkContext { get { var workContext = (WorkContext)System.Web.HttpContext.Current.Items["__current__workcontext"]; if (workContext == null) { workContext = new WorkContext(); System.Web.HttpContext.Current.Items["__current__workcontext"] = workContext; } return workContext; } } public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext) { if (WorkContext == null || WorkContext.UserInfo == null || WorkContext.UserInfo.UserID == 0) { if (filterContext == null) { throw new ArgumentNullException("filterContext"); } if (filterContext.HttpContext.Request.IsAjaxRequest()) { filterContext.HttpContext.Response.StatusCode = 401; string strUrl = ConfigurationManager.AppSettings.Get("PassportDoMain"); if (filterContext.HttpContext.Request.Url != null) { string path = filterContext.HttpContext.Request.Url.ParserUrl(); strUrl += "?returnUrl=" + path; } filterContext.Result = Ajax.Json(new { succeed = false, ret = 401, url = strUrl }, JsonRequestBehavior.AllowGet); return; } if (filterContext.HttpContext.Request.Url != null) { string path = filterContext.HttpContext.Request.Url.ParserUrl(); string strUrl = ConfigurationManager.AppSettings.Get("PassportDoMain") + "?returnUrl={0}"; filterContext.HttpContext.Response.Redirect(string.Format(strUrl, HttpUtility.UrlEncode(path)), true); filterContext.HttpContext.Response.End(); } } } }
3.AUTOMAPPER
public static class AutoMapperExtension { /// <summary> /// 类型映射 /// </summary> public static T MapperTo<T>(this object obj) { if (obj == null) return default(T); Mapper.CreateMap(obj.GetType(), typeof(T)); return Mapper.Map<T>(obj); } /// <summary> /// 集合列表类型映射 /// </summary> public static List<TDestination> MapperToToList<TDestination>(this IEnumerable source) { foreach (var first in source) { var type = first.GetType(); Mapper.CreateMap(type, typeof(TDestination)); break; } return Mapper.Map<List<TDestination>>(source); } /// <summary> /// 集合列表类型映射 /// </summary> public static List<TDestination> MapperToToList<TSource, TDestination>(this IEnumerable<TSource> source) { Mapper.CreateMap<TSource, TDestination>(); return Mapper.Map<List<TDestination>>(source); } /// <summary> /// 类型映射 /// </summary> public static TDestination MapperTo<TSource, TDestination>(this TSource source, TDestination destination) where TSource : class where TDestination : class { if (source == null) return destination; Mapper.CreateMap<TSource, TDestination>(); return Mapper.Map(source, destination); } }