當session timeout時, 若不作處理, 運作時有機會throw exception. 為應對此情況, 可以自訂一個Attribute, 當controller / action call 時, 便會先行attribute.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace Test.Attributes { public class SessionExpireAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpContext ctx = HttpContext.Current; // check sessions here if (HttpContext.Current.Session["userName"] == null) { filterContext.Result = new RedirectResult("~/Account/Login"); return; } base.OnActionExecuting(filterContext); } } }
Leave a Reply