爱上MVC3系列~同步与异步提交,在过滤器里如何进行重定向~续
上一篇文章已经解决了同步与异步表单提交的页面重定向问题,而为什么要写个“续”呢,原因是,我觉得上次的代码不够美丽,而且有些啰嗦,也不符合我的代码之美原则,所以,我自己的代码进行了一次重构:
两件事:
同步表单,返回view或者partialview时,事实上返回的是一个text/html格式的视图
异步表单,返回Json,text,xml,javascript时,事实上返回的是一个text/javascript格式的视图
有了上面的基础,我们把代码重构一下吧:
1 /// <summary> 2 /// 角色过滤器 3 /// </summary> 4 public class RoleFilter : AuthorizeAttribute 5 { 6 const string USER_WARN = "您必须先登陆才能执行本操作"; 7 const string WORKSHOPUSER_WARN = "必须是本工作室的用户才能执行本操作"; 8 const string WORKSHOPTEACHER_WARN = "必须是本工作室的老师才能执行本操作"; 9 RoleFlag _RoleFlag; 10 /// <summary> 11 /// 角色过滤器构造依法 12 /// </summary> 13 /// <param name="roleFlag">角色类型</param> 14 public RoleFilter(RoleFlag roleFlag) 15 { 16 _RoleFlag = roleFlag; 17 } 18 public override void OnAuthorization(AuthorizationContext filterContext) 19 { 20 var userID = filterContext.RequestContext.HttpContext.Session["UserID"]; 21 var request = filterContext.RequestContext.HttpContext.Request; 22 var routeData = filterContext.RequestContext.RouteData.Values["id"]; 23 //同步方式 24 string htmlMsg = @"<html><head><title></title></head><body> 25 <link href='/Scripts/jquery-plugin-boxy/css/boxy.css' rel='stylesheet' type='text/css' /> 26 <script src='/Scripts/jquery-1.4.4.min.js' type='text/javascript'></script> 27 <script src='/Scripts/jquery-plugin-boxy/js/jquery.boxy.js' type='text/javascript'></script> 28 <script src='/Scripts/jquery-plugin-boxy/jBoxyFun.js' type='text/javascript'></script> 29 <script type='text/javascript'> 30 $(function(){ 31 alertHrefFun('msg','url');});</script></body></html>"; 32 //异步方式 33 string jsMsg = @"alertHrefFun('msg','url');"; 34 35 switch (this._RoleFlag) 36 { 37 case RoleFlag.User: 38 if (userID == null || Convert.ToInt32(userID) <= 0) 39 { 40 var result = new ContentResult 41 { 42 Content = htmlMsg.Replace("msg", USER_WARN).Replace("url", request.UrlReferrer.ToString()), 43 ContentType = "text/html", 44 }; 45 if (request.IsAjaxRequest()) 46 { 47 result.Content = jsMsg.Replace("msg", USER_WARN).Replace("url", request.UrlReferrer.ToString()); 48 result.ContentType = "text/javascript"; 49 } 50 filterContext.Result = result; 51 } 52 53 break; 54 default: 55 break; 56 } 57 } 58 }
如果我们觉得代码还是显示有点不完善,我们可以把构建ContentResult对象的语句提取到一个方法上,把常量提升到类级别上,看修改后的代码:
1 /// <summary> 2 /// 角色过滤器 3 /// </summary> 4 public class RoleFilter : AuthorizeAttribute 5 { 6 #region Constructs 7 /// <summary> 8 /// 角色过滤器构造依法 9 /// </summary> 10 /// <param name="roleFlag">角色类型</param> 11 public RoleFilter(RoleFlag roleFlag) 12 { 13 _RoleFlag = roleFlag; 14 } 15 #endregion 16 17 #region Consts 18 /// <summary> 19 /// 必须登陆警告 20 /// </summary> 21 const string USER_WARN = "您必须先登陆才能执行本操作"; 22 /// <summary> 23 /// 必须是工作室成员警告 24 /// </summary> 25 const string WORKSHOPUSER_WARN = "必须是本工作室的用户才能执行本操作"; 26 /// <summary> 27 /// 必须是工作室老师警告 28 /// </summary> 29 const string WORKSHOPTEACHER_WARN = "必须是本工作室的老师才能执行本操作"; 30 #endregion 31 32 #region Fields 33 /// <summary> 34 /// 同步方式 35 /// </summary> 36 string htmlMsg = @"<html><head><title></title></head><body> 37 <link href='/Scripts/jquery-plugin-boxy/css/boxy.css' rel='stylesheet' type='text/css' /> 38 <script src='/Scripts/jquery-1.4.4.min.js' type='text/javascript'></script> 39 <script src='/Scripts/jquery-plugin-boxy/js/jquery.boxy.js' type='text/javascript'></script> 40 <script src='/Scripts/jquery-plugin-boxy/jBoxyFun.js' type='text/javascript'></script> 41 <script type='text/javascript'> 42 $(function(){ 43 alertHrefFun('msg','url');});</script></body></html>"; 44 /// <summary> 45 /// 异步方式 46 /// </summary> 47 string jsMsg = @"alertHrefFun('msg','url');"; 48 49 RoleFlag _RoleFlag; 50 #endregion 51 52 /// <summary> 53 /// 根据请求上下文类型,返回对象 54 /// </summary> 55 /// <param name="request"></param> 56 /// <param name="jsMsg"></param> 57 /// <param name="htmlMsg"></param> 58 /// <returns></returns> 59 ActionResult GetContentResult(HttpRequestBase request, string Warn) 60 { 61 var result = new ContentResult 62 { 63 Content = htmlMsg.Replace("msg", Warn).Replace("url", request.UrlReferrer.ToString()), 64 ContentType = "text/html", 65 }; 66 if (request.IsAjaxRequest()) 67 { 68 result.Content = jsMsg.Replace("msg", Warn).Replace("url", request.UrlReferrer.ToString()); 69 result.ContentType = "text/javascript"; 70 } 71 return result; 72 } 73 74 public override void OnAuthorization(AuthorizationContext filterContext) 75 { 76 var userID = filterContext.RequestContext.HttpContext.Session["UserID"]; 77 var request = filterContext.RequestContext.HttpContext.Request; 78 var routeData = filterContext.RequestContext.RouteData.Values["id"]; 79 switch (this._RoleFlag) 80 { 81 case RoleFlag.User: 82 if (userID == null || Convert.ToInt32(userID) <= 0) 83 { 84 filterContext.Result = GetContentResult(request, USER_WARN); 85 } 86 87 break; 88 default: 89 break; 90 } 91 } 92 }
值得注意的是:如果是返回text/html页面,然原来的视图将被覆盖,所以你的JS文件与CSS文件需要重新被引用才行,但如果是返回text/javascript对象,则它会使用原页面的脚本文件与样式文件,这一点要清楚。