B2K
B2K

Reputation: 2611

Cannot redirect after HTTP headers have been sent MVC3

I am getting this error in an MVC3 application when using the RedirectPermanent(url) method on a base controller. Buffering the output, suggested by other StackOverflow answers, has had no effect.

protected void Application_BeginRequest(object sender, EventArgs e)
{
   Context.Response.BufferOutput = true;
}

The redirect is triggered as the first action result on the default controller on this web site: http://www.autoquoter.com

I don't have any code that directly adds a header. That being the case, is there any way to determine what is adding a response header?

Here is the network log from webkit's debugger for the first page of the web site.

Request URL:http://www.autoquoter.com/
Request Method:GET
Status Code:301 Moved Permanently

Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
User-Agent:Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2

Response Headers
Cache-Control:no-cache, no-store, must-revalidate
Content-Length:128
Content-Type:text/html; charset=utf-8
Date:Thu, 21 Mar 2013 17:10:38 GMT
Expires:-1
Location:/aq/en/Home
Pragma:no-cache
Server:Microsoft-IIS/6.0
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET

Upvotes: 1

Views: 7187

Answers (1)

B2K
B2K

Reputation: 2611

Looks like I have solved this. There was another redirect based upon host name buried inside of a NoCacheAttribute filter. This was setting properties on Response object in the OnResultExecuting method.

I renamed the method to OnActionExecuting so it would be triggered sooner, and replaced a manual redirect with a RedirectResult. I also now avoid updating the cache settings if I'm already redirecting.

Before:

 if (currentHost != prefHost && filterContext.HttpContext.Response.StatusCode != 301)
 {
      var Url = filterContext.HttpContext.Request.Url.Scheme + "://" + prefHost + filterContext.HttpContext.Request.Url.PathAndQuery;
      filterContext.HttpContext.Response.StatusCode = 301;
      filterContext.HttpContext.Response.RedirectLocation = Url;
      filterContext.HttpContext.Response.End();
      return;
 }
 filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); 
 filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false); 
 filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches); 
 filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); 
 filterContext.HttpContext.Response.Cache.SetNoStore(); 

After:

 if (currentHost != prefHost && filterContext.HttpContext.Response.StatusCode != 301)
 {
      var Url = filterContext.HttpContext.Request.Url.Scheme + "://" + prefHost + filterContext.HttpContext.Request.Url.PathAndQuery;
      filterContext.Result = new RedirectResult(Url, true);
      disableCache = false;
 }


    if (disableCache)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
        filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
        filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        filterContext.HttpContext.Response.Cache.SetNoStore();
    }
    base.OnActionExecuting(filterContext);

Upvotes: 2

Related Questions