0
我在我的應用程序控制器執行OnActionExecuted,檢查用戶是否仍然登錄。當用戶不再登錄會話需要被銷燬並被重新引導回主頁而不必進入新頁面。 (所以當前的請求必須被取消)。ASP.NET MVC-服務器無法修改HTTP頭後發送的Cookie
這是我的代碼:
if (System.Web.HttpContext.Current.Session.IsNewSession)
{
if (System.Web.HttpContext.Current.Session["Userid"] != null)
{
// Check userid session
filterContext.Controller.TempData["sessionError"] = 1;
System.Web.HttpContext.Current.Session.RemoveAll();
System.Web.HttpContext.Current.Response.BufferOutput = true;
System.Web.HttpContext.Current.Response.Flush();
RedirectToAction("Index", "Home");
return;
}
}
但是我這段代碼獲得的問題是,它給了我一個例外:
Message:
Server cannot modify cookies after HTTP headers have been sent.
Source:
at System.Web.HttpResponse.BeforeCookieCollectionChange()
at System.Web.HttpCookieCollection.Add(HttpCookie cookie)
at Topsite.Classes.GlobalFunctions.SetCookie(String name, String value, DateTime endDate)
at Topsite.Controllers.ApplicationController.OnActionExecuted(ActionExecutedContext filterContext)
at System.Web.Mvc.Controller.System.Web.Mvc.IActionFilter.OnActionExecuted(ActionExecutedContext filterContext)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
有什麼建議?提前致謝。
我確實發現了一些其他問題,有同樣的問題,我跟着他們,但我仍然收到異常。這就是我尋求幫助的原因。