Please enable Javascript for better experience...
Error: Server cannot set status after HTTP headers have been sent-MVC
By Big | Oct 9, 2015 | In Tips | Total Views [ 8069 ]
Taged In
(0 Like)
Rate

To Overcome the error: Server cannot set status after HTTP headers have been sent in MVC try to avoid Response.Redirect() and use RedirectResult() instead. 

The Problem

Many time while calling a method in my MVC project, I was facing this error: Server cannot set status after HTTP headers have been sent . I was not able to encounter why this error comes, what is the reason behind it. And the main hurdle was, it runs fine most of the time so you even don't know when your server invites this error. Many time you will face this issue on production environment. There is little tips which can save your time.

The Solution

The solution for this problem would be: always try to use RedirectResult() method to redirect to a url instead of Response.Redirect() as Response.Redirect() is not part of MVC pipeline.

Response.Redirect() is not part of MVC pipeline.

Explaination

The possible reason behind this error can be:

  • When action was executing, it was trying to write markup to response stream.
  • The stream was unbuffered forcing the response headers to get written before the markup writing could begin.
  • View encountered a runtime error and Exception handler (HandleErrorAttribute()) execute in trying to set the status code to other than 200.
  • Execution fails because the headers have already been sent.

You can over come this problem either setting setting BufferOutput to true before any action execute.

   Response.BufferOutput = true;

Or you should use RedirectResult() method to redirect to a url instead of Response.Redirect()

In my case I was redirecting to url using Response.Redirect() method. Let see this with an example. In below code I have created a custom attribute and overriding OnActionExecuting() method of ActionFilterAttribute attribute. Below is the code.

  public class CustomFilterAttribute : ActionFilterAttribute
{
   public override void OnActionExecuting(ActionExecutingContext filterContext)
   {
   if (System.Web.HttpContext.Current.User == null || !System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
   {
string url = "~/Member/Login?ReturnUrl=" + HttpContext.Current.Request.Url.AbsolutePath;
   HttpContext.Current.Response.Redirect(url);
   }
}
}

To overcome this issue you just need to replace your redirect method. See the code below.

 public class CustomFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (System.Web.HttpContext.Current.User == null || !System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
string url = "~/Member/Login?ReturnUrl=" + HttpContext.Current.Request.Url.AbsolutePath;
filterContext.Result = new RedirectResult(url);
}
}
}

Hope it can help you. This simple line of code can save your many hours.

Share this

About the Author

Big
Rahul Jha (Big)
Founder, Developer dotnet-concept.com
Has working experience in different phases of Software Development Life Cycle (SDLC) in CMS, Gaming, Health Care and Financial Services domain using Agile pattern. Working experience in Design patterns, ASP.NET, MVC, Windows application, WCF, ADO.NET, SQL Server and Test Driven Development (TDD) environment with JQuery, JavaScript, N-Unit, Entity Frameworks, LINQ, Code Refactoring and Business Objects Models.

 
Please SignUp/Login to comment...

Or comment as anonymous...
* Name
* Email ID
Comment