4

I have OnActionExecuting ActionFilter in which i have checked whether the user session has been expired or not, and in case when the session is expired, then the user must be redirected to the Login Page.

CompressFilterAttribute.cs file has the code as shown below:

public override void OnActionExecuting(ActionExecutingContext FilterContext)
{
    if (((((System.Web.Mvc.ControllerContext)(FilterContext)).HttpContext).Request).IsAuthenticated)
            GZipEncodePage(); //Function to check Session Expiration Time

            var action = (string)FilterContext.RouteData.Values["action"];

            if (!FilterContext.HttpContext.User.Identity.IsAuthenticated && 
                action != "Index" && 
                action != "FindStoreNameByText" && 
                action.ToLower() != "sessiontimeout" && 
                action.ToLower() != "logout")
            {
                string UrlSucesso = "/Home";
                string UrlRedirecionar = string.Format("?ReturnUrl={0}", UrlSucesso);
                string UrlLogin = FormsAuthentication.LoginUrl + UrlRedirecionar;
                FilterContext.HttpContext.Response.Redirect(UrlSucesso, true);
            }
    }

CustomErrorHandleAttribute.cs file has the code as shown below:

public override void OnException(ExceptionContext filterContext)
{
    base.OnException(filterContext);
    // Rest logic
}

In CustomErrorHandleAttribute.cs file I am getting the error --> server cannot set status after http headers have been sent

Please help me on this.

tereško
  • 58,060
  • 25
  • 98
  • 150
HarshSharma
  • 630
  • 3
  • 9
  • 34
  • 1
    possible duplicate of [Server cannot set status after HTTP headers have been sent IIS7.5](http://stackoverflow.com/questions/2383169/server-cannot-set-status-after-http-headers-have-been-sent-iis7-5) – drzaus Dec 04 '14 at 14:13

1 Answers1

9

Your OnActionExecution method sends the necessary redirection http headers, but It does not stop the controller code from executing.

I think you should use the following to redirect the user instead of Response.Redirect:

 FilterContext.Result = new RedirectResult(url); 

See also these other stack overflow questions:

Community
  • 1
  • 1
artokai
  • 405
  • 3
  • 8
  • Thanks for your help, but i dnt solve the issue still the same error anyways i am marking the answer as helpful,... – HarshSharma Jan 09 '14 at 09:47
  • 1
    I still think that the problems occurs because the CustomErrorHandleAttribute.cs is trying to send an http error after the redirection header has already been sent to the browser. You should probably debug what code runs after the redirection and what is the exact exception that is stored in the [ExceptionContext.Exception](http://msdn.microsoft.com/en-us/library/system.web.mvc.exceptioncontext.exception(v=vs.118).aspx) – artokai Jan 09 '14 at 11:08