0

I've got an MVC Site that is giving an "Server cannot modify cookies after HTTP headers have been sent." error. It's happening in the _Layout.cshtml. At that line, there is a @Html.Action("_LightBoxes"), which calls a _LightBoxes partial view. The code for the _LightBoxes Partial View is simply:-

[ChildActionOnly]
public PartialViewResult _LightBoxes()
{
    return PartialView();
}

I've read other threads with this error, but these all seem to be performing re-directs or similar, where as in this controller it simply returns an empty Partial View.

Does anyone have any suggestions for what is causing this? Could it be a reference inside that PartialView to another controller which is actually causing this error?

EDIT - 26 JANUARY 2015 I think this might be the issue. This is in a Partial View called from within the PartialView at that line. Code below, for a Facebook login button, which has cookies set to true.

<script type="text/javascript">
    //Facebook Login Button
    window.fbAsyncInit = function () {
        FB.init({
            appId: "@(System.Configuration.ConfigurationManager.AppSettings["facebook_appid"].ToString())", // App ID
            //channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // Channel File
            status: true, // check login status
            cookie: false, // enable cookies to allow the server to access the session
            oauth: true, // enable OAuth 2.0
            xfbml: true  // parse XFBML
        });
        $("fblogin").show();
    };
    (function (d) {
        var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; }
        js = d.createElement('script'); js.id = id; js.async = true;
        js.src = "//connect.facebook.net/en_US/all.js";
        d.getElementsByTagName('head')[0].appendChild(js);
    }(document));
    //End Facebook
</script>

Thanks, Mike.

Mike Upjohn
  • 1,251
  • 2
  • 16
  • 38
  • You are trying to modify the HttpCookie when server has been completely send headers to the client browser. Look at this answer http://stackoverflow.com/questions/2383169/server-cannot-set-status-after-http-headers-have-been-sent-iis7-5 – Anirudha Gupta Jan 23 '15 at 15:29
  • And what's in your _LightBoxes.cshtml? – Erik Funkenbusch Jan 23 '15 at 16:13

1 Answers1

0

This is often caused in MVC applications by inappropriate use of Response.Write() somewhere in your application (it doesn't need to even be in the code that is causing the problem). MVC is designed to render pages AFTER the controllers have finished processing, and it renders these to an output buffer and finally writes this to the response at the very end of the output.

If you have code somewhere in your pipeline that executes a Response.Write() and then after that you attempt to write a cookie, but before the view has actually been rendered you get this problem.

So search you code for Response.Write() that is not in a view.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • Thanks for this. We have a utilities project of common functions, which is dispatched as a DLL with the website. Response.Write() in there might cause this? – Mike Upjohn Jan 23 '15 at 16:46
  • @MikeUpjohn - since I can't see your code, and I don't know how it's called, I don't know.. But if there is Response.Write() that gets called from controller methods (rather than in views) then yes, its possible. Since, it looks like you have a view that has code that tries to write a cookie (your _LightBox.cshtml). In general, you should not write directly to the Response in MVC, especially not outside of a view. – Erik Funkenbusch Jan 23 '15 at 16:51
  • Yes, I did find one so I've changed this and see if the issue stops over the weekend. Thanks! – Mike Upjohn Jan 23 '15 at 17:11
  • I've just updated the above with some new code which I think might be causing the issue? – Mike Upjohn Jan 26 '15 at 13:51
  • @MikeUpjohn - that's unlikely as that will be a totally different request made by facebook. That has nothing to do with the request coming from your server. – Erik Funkenbusch Jan 26 '15 at 15:28