ASP.NET MVC过滤器
用ASP.NET MVC重写Authoriz过滤器用于指定方法的验证,如验证用户是否登陆方法如下当用户为异步操作时可以返回指定类型的结果如json或指定类型
/// <summary> /// 验证登陆过滤 /// </summary> public class LoginAuthorizeAttribute : AuthorizeAttribute { private IAuthenticationService _authencation; public LoginAuthorizeAttribute() { _authencation = new FormsAuthenticationService(); } public override void OnAuthorization(AuthorizationContext filterContext) { base.OnAuthorization(filterContext); //如当前请求为ajax请求时返回指定错误json if (!filterContext.HttpContext.User.Identity.IsAuthenticated && (new HttpRequestWrapper(HttpContext.Current.Request)).IsAjaxRequest()) { filterContext.Result = new JsonResult() { Data=new ResultModel(ResultType.UnAuthorize), JsonRequestBehavior=JsonRequestBehavior.AllowGet }; } } }
Application处理ajax的异常:当异步请求某页面时如果后台运行处理过程抛出异常或者错误时请求时,只会得到一个500内部服务器错误的页面,不开启浏览器的开发者模式的是看不到任何返回的,由此可在Global.asax中写上以下方法,实现返回指定结果
protected void Application_Error(Object sender, EventArgs e) { var lastError = Server.GetLastError(); if (lastError != null) { CNBlogs.Infrastructure.Logging.Logger.Default.Error("Application_Error", lastError); if(Request != null && (new HttpRequestWrapper(Request)).IsAjaxRequest()) { Response.Clear(); Response.ContentType = "application/json; charset=utf-8"; Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject( new { isSuccess = false, message = lastError.Message })); Response.Flush(); Server.ClearError(); return; } Response.StatusCode = 500; Server.ClearError(); } }
ajax客户端收到的结果如下:
从此,可以尽情地抛异常了。
ASP.NET MVC使用GZIP压缩时过滤器如下
public class CompressAttribute:ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"]; if (!string.IsNullOrEmpty(acceptEncoding)) { acceptEncoding = acceptEncoding.ToLower(); var response = filterContext.HttpContext.Response; if (acceptEncoding.Contains("gzip")) { response.AppendHeader("Content-encoding", "gzip"); response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); } else if (acceptEncoding.Contains("deflate")) { response.AppendHeader("Content-encoding", "deflate"); response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); } } } }
代码很简单了,就是重写了过滤器中的OnActionExecuting 就是在Action每次执行前先执行OnActionExecuting。
public class CompressAttribute:ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"]; if (!string.IsNullOrEmpty(acceptEncoding)) { acceptEncoding = acceptEncoding.ToLower(); var response = filterContext.HttpContext.Response; if (acceptEncoding.Contains("gzip")) { response.AppendHeader("Content-encoding", "gzip"); response.Filter = new GZipStream(response.Filter, CompressionMode.Compress); } else if (acceptEncoding.Contains("deflate")) { response.AppendHeader("Content-encoding", "deflate"); response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress); } } } }
因为它CompressAttribute过滤器就可以在对应的需要压缩数据的Action上进行标识
[Compress] public ActionResult Index() { ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(); }
记录当时一闪而过的某个想法亦或是读过每篇文字的感悟!