在我的 asp.net MVC 應用程式中,在布區域分,有一個組合框,我從會話中為其加載資料。
如果會話資料為空,我想從會話中注銷用戶。所以我找到了這個類并將其添加到我的專案中。
public class SessionExpireAttribute: ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var context = filterContext.HttpContext;
HttpContext ctx = HttpContext.Current;
string sessionCookie = context.Request.Headers["Cookie"];
// check sessions here
if (sessionCookie == null)
{
filterContext.Result = new RedirectResult("~/Account/LogOff");
return;
}
base.OnActionExecuting(filterContext);
}
}
我在控制器的頂部添加了這個屬性。
[HandleError]
[SessionExpire]
public class TaskMainsController : Controller
{
每當會話資料為空時,用戶都會從系統注銷并再次重定向到登錄。
但是,在布局組合框中,如果會話資料為空,則不會從系統注銷,然后session data null exception觸發。如何避免這種情況并處理這種錯誤?
該應用程式即將上線,但我找不到任何解決方案。
uj5u.com熱心網友回復:
我認為您只需要在整個專案的控制器上應用此過濾器。為此,您可以在 Global.asax.cs 類的 GlobalFilters 中添加此過濾器:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
GlobalFilters.Filters.Add(new SessionExpireAttribute());
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/534475.html
標籤:C#网站asp.net-mvcasp.net-mvc-4
上一篇:傳遞到ViewDataDictionary的模型項屬于型別,但此ViewDataDictionary實體需要型別為“System.Collections”的模型項
