遇到這個需求,想到的第一點就是,這個確定是須要寫在一個通用的地方。方便調用。通常能夠定義個 父類控制器在OnActionExcuting方法執行前寫邏輯,先上代碼,一邊寫代碼一邊講解:ajax
/// <summary> /// 執行方法前 /// </summary> /// <param name="filterContext"></param> protected override void OnActionExecuting(ActionExecutingContext filterContext) { if (filterContext.IsChildAction) return; //須要排除校驗的控制器名稱 string[] excludeControllerName = { "error", "ThirdPartyNotify" }; bool hasElenemt = MyCommFun.StringHasElenemt(filterContext.Controller.ToString().ToLower(), excludeControllerName); if (hasElenemt) { //排除error控制器 return; } string userAgent = Request.UserAgent; //若是爲微信端登陸,則須要網頁受權,排除error控制器 if (userAgent.ToLower().IndexOf("micromessenger") > -1) { if (WebHelper.IsAjax())//判斷是否爲ajax請求的狀況 { ProcessInvalidUser_Ajax(filterContext); return; } else {//網頁受權 bool obpOk= OAuth2BaseProc(filterContext, wid); if (!obpOk) { return; } } } base.OnActionExecuting(filterContext); }